source: abuse/branches/lol/src/gamma.cpp @ 732

Last change on this file since 732 was 732, checked in by Sam Hocevar, 8 years ago

build: SDL2 compilation fixes.

File size: 5.4 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2013 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#if HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <math.h>
16
17#include "common.h"
18
19#include "lisp/lisp.h"
20
21#include "imlib/window.h"
22#include "imlib/scroller.h"
23#include "imlib/dprint.h"
24
25#include "game.h"
26#include "id.h"
27#include "cache.h"
28#include "loader2.h"
29
30extern int dev_ok;
31Palette *old_pal = NULL;
32
33class AGrayPicker : public AScrollPicker
34{
35public:
36    AGrayPicker(ivec2 pos, int id, int start, int current)
37      : AScrollPicker(pos, id, 1, 20, 0, 0)
38    {
39        cur_sel = current;
40        sc = start;
41        reconfigure();
42        cur_sel = current;
43    }
44
45    int sc;
46    virtual void DrawItem(AImage *screen, ivec2 pos, int num, int active)
47    {
48        ivec2 corner = pos + ivec2(item_width() - 1, item_height() - 1);
49        screen->Bar(pos, corner, 0);
50        screen->Bar(pos, corner - ivec2(3, 0), sc + num);
51        if(active)
52        {
53            screen->Rectangle(pos, corner, 255);
54        }
55    }
56    void set_pos(int x) { cur_sel = x; }
57    virtual int total() { return 32; }
58    virtual int item_width() { return 12; }
59    virtual int item_height() { return 20; }
60    virtual int activate_on_mouse_move() { return 0; }
61};
62
63static char const *lang_string(char const *symbol)
64{
65    LSymbol *v = LSymbol::Find(symbol);
66    if (!v || !DEFINEDP(v->GetValue()))
67        return "Language symbol missing!";
68    return lstring_value(v->GetValue());
69}
70
71void gamma_correct(Palette *&pal, int force_menu)
72{
73    long dg=0,old_dg=0;
74    int abort=0;
75
76    // see if user has already done this routine
77    LSymbol *gs = LSymbol::Find("darkest_gray");
78
79    if(old_pal)
80    {
81        delete pal;
82        pal = old_pal;
83        old_pal = nullptr;
84    }
85
86    if(gs && DEFINEDP(gs->GetValue()) && !force_menu)
87    {
88        dg = lnumber_value(gs->GetValue());
89    }
90    else
91    {
92        if(gs && DEFINEDP(gs->GetValue()))
93        {
94            dg = old_dg = lnumber_value(gs->GetValue());
95        }
96        // load in a fine gray palette they can chose from
97        Palette *gray_pal = pal->Copy();
98        int tc = 32;
99
100        for(int i = 0; i < tc; i++)
101            gray_pal->SetColor(i, u8vec3(i * 4));
102
103        gray_pal->Load();
104
105        int wm_bc = wm->bright_color(), wm_mc = wm->medium_color(), wm_dc = wm->dark_color();
106
107        u8vec3 br = (u8vec3)min((ivec3)pal->GetColor(wm_bc), ivec3(255 - 20)) + u8vec3(20);
108        u8vec3 md = (u8vec3)max((ivec3)pal->GetColor(wm_mc), ivec3(20)) - u8vec3(20);
109        u8vec3 dr = (u8vec3)max((ivec3)pal->GetColor(wm_dc), ivec3(40)) - u8vec3(40);
110
111        wm->set_colors(gray_pal->FindClosest(br),
112                       gray_pal->FindClosest(md),
113                       gray_pal->FindClosest(dr));
114
115        int sh = wm->font()->Size().y + 35;
116
117        AWidgetList widgets;
118        widgets << new AButton(ivec2(5, 5 + sh * 3), ID_GAMMA_OK, cache.img(ok_button));
119        widgets << new AInfoField(ivec2(35, 10 + sh * 3), ID_NULL, lang_string("gamma_msg"));
120
121        AGrayPicker *gp = new AGrayPicker(ivec2(2, 5 + sh), ID_GREEN_PICKER, 0, dg / 4);
122        gp->set_pos(dg / 4);
123        widgets << gp;
124
125        AWindow *gw = wm->CreateWindow(ivec2(xres / 2 - 190,
126                                             yres / 2 - 90), ivec2(-1), "", widgets);
127
128        Event ev;
129        wm->flush_screen();
130        do
131        {
132            do
133            {
134                wm->get_event(ev);
135            } while(ev.type == EV_MOUSE_MOVE && wm->IsPending());
136            wm->flush_screen();
137            if(ev.type == EV_CLOSE_WINDOW)
138                abort = 1;
139            if(ev.type == EV_KEY && ev.key == JK_ESC)
140                abort = 1;
141        } while(!abort && (ev.type != EV_MESSAGE || ev.message.id != ID_GAMMA_OK));
142
143        dg = ((AScrollPicker *)gw->inm->get(ID_GREEN_PICKER))->first_selected() * 4;
144
145        wm->close_window(gw);
146        wm->flush_screen();
147
148        wm->set_colors(wm_bc, wm_mc, wm_dc);
149        delete gray_pal;
150
151        if(!abort)
152        {
153            char *gammapath;
154            FILE *fp;
155
156            gammapath = (char *)malloc(strlen(get_save_filename_prefix()) + 10);
157            sprintf(gammapath, "%sgamma.lsp", get_save_filename_prefix());
158            fp = open_FILE(gammapath, "wb");
159            if(fp)
160            {
161                fprintf(fp, "(setq darkest_gray %ld)\n", dg);
162                fclose(fp);
163                LSpace *sp = LSpace::Current;
164                LSpace::Current = &LSpace::Perm;
165                LSymbol::FindOrCreate("darkest_gray")->SetNumber(dg);
166
167                LSpace::Current = sp;
168            }
169            else
170            {
171                dprintf("Unable to write to file gamma.lsp\n");
172            }
173            free(gammapath);
174        }
175    }
176
177    if(abort)
178        dg = old_dg;
179
180    if(dg < 1)
181        dg = 1;
182    else if(dg > 128)
183        dg = 128;
184
185    double gamma = log(dg / 255.0) / log(16.0 / 255.0);
186
187    old_pal = pal;
188    pal = new Palette;
189    for(int i = 0; i < 256; i++)
190    {
191        u8vec3 old = old_pal->GetColor(i);
192        pal->SetColor(i, u8vec3(lol::pow(old.r / 255.0, gamma) * 255,
193                                lol::pow(old.g / 255.0, gamma) * 255,
194                                lol::pow(old.b / 255.0, gamma) * 255));
195    }
196
197    pal->Load();
198}
Note: See TracBrowser for help on using the repository browser.