source: abuse/trunk/src/gamma.cpp @ 682

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

core: rename vec2i to ivec2 and update matrix.h from Lol Engine.

File size: 5.9 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 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 defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <math.h>
16
17#include "common.h"
18
19#include "game.h"
20
21#include "jwindow.h"
22#include "lisp.h"
23#include "scroller.h"
24#include "id.h"
25#include "cache.h"
26#include "dprint.h"
27#include "loader2.h"
28
29extern int dev_ok;
30palette *old_pal = NULL;
31
32class gray_picker : public spicker
33{
34public:
35    int sc;
36    virtual void draw_item(image *screen, int x, int y, int num, int active)
37    {
38        long x2 = x + item_width() - 1;
39        long y2 = y + item_height() - 1;
40        screen->Bar(ivec2(x, y), ivec2(x2, y2), 0);
41        screen->Bar(ivec2(x, y), ivec2(x2 - 3, y2), sc + num);
42        if(active)
43        {
44            screen->Rectangle(ivec2(x, y), ivec2(x2, y2), 255);
45        }
46    }
47    void set_pos(int x) { cur_sel = x; }
48    virtual int total() { return 32; }
49    virtual int item_width() { return 12; }
50    virtual int item_height() { return 20; }
51    virtual int activate_on_mouse_move() { return 0; }
52
53    gray_picker(int X, int Y, int ID, int start, int current, ifield *Next) : spicker(X, Y, ID, 1, 20, 0, 0, Next)
54    {
55        cur_sel = current;
56        sc = start;
57        reconfigure();
58        cur_sel=current;
59    }
60};
61
62static char const *lang_string(char const *symbol)
63{
64    LSymbol *v = LSymbol::Find(symbol);
65    if (!v || !DEFINEDP(v->GetValue()))
66        return "Language symbol missing!";
67    return lstring_value(v->GetValue());
68}
69
70void gamma_correct(palette *&pal, int force_menu)
71{
72    long dg=0,old_dg=0;
73    int abort=0;
74
75    // see if user has already done this routine
76    LSymbol *gs = LSymbol::Find("darkest_gray");
77
78    if(old_pal)
79    {
80        delete pal;
81        pal = old_pal;
82        old_pal = NULL;
83    }
84
85    if(gs && DEFINEDP(gs->GetValue()) && !force_menu)
86    {
87        dg = lnumber_value(gs->GetValue());
88    }
89    else
90    {
91        if(gs && DEFINEDP(gs->GetValue()))
92        {
93            dg = old_dg = lnumber_value(gs->GetValue());
94        }
95        // load in a fine gray palette they can chose from
96        palette *gray_pal = pal->copy();
97        int i = 0;
98        int tc = 32;
99
100        for(; i < tc; i++)
101        {
102            gray_pal->set(i, i * 4, i * 4, i * 4);
103        }
104
105        gray_pal->load();
106
107        int wm_bc = wm->bright_color(), wm_mc = wm->medium_color(), wm_dc = wm->dark_color();
108
109        int br_r = pal->red(wm_bc) + 20;
110        if(br_r > 255)
111            br_r = 255;
112        int br_g = pal->green(wm_bc) + 20;
113        if(br_g > 255)
114            br_g = 255;
115        int br_b = pal->blue(wm_bc) + 20;
116        if(br_b > 255)
117            br_b = 255;
118
119        int md_r = pal->red(wm_mc) - 20;
120        if(md_r < 0)
121            md_r = 0;
122        int md_g = pal->green(wm_mc) - 20;
123        if(md_g < 0)
124            md_g = 0;
125        int md_b = pal->blue(wm_mc) - 20;
126        if(md_b < 0)
127            md_b = 0;
128
129        int dr_r = pal->red(wm_dc) - 40;
130        if(dr_r < 0)
131            dr_r = 0;
132        int dr_g = pal->green(wm_dc) - 40;
133        if(dr_g < 0)
134            dr_g = 0;
135        int dr_b = pal->blue(wm_dc) - 40;
136        if(dr_b < 0)
137            dr_b = 0;
138
139        wm->set_colors(gray_pal->find_closest(br_r, br_g, br_b),
140            gray_pal->find_closest(md_r, md_g, md_b),
141            gray_pal->find_closest(dr_r, dr_g, dr_b));
142
143        int sh = wm->font()->Size().y + 35;
144        button *but = new button(5, 5 + sh * 3, ID_GAMMA_OK, cache.img(ok_button),
145            new info_field(35, 10 + sh * 3, ID_NULL, lang_string("gamma_msg"), 0));
146
147        gray_picker *gp = new gray_picker(2, 5 + sh, ID_GREEN_PICKER, 0, dg / 4, but);
148        gp->set_pos(dg / 4);
149
150        Jwindow *gw = wm->CreateWindow(ivec2(xres / 2 - 190,
151                                             yres / 2 - 90), ivec2(-1), gp);
152
153        Event ev;
154        wm->flush_screen();
155        do
156        {
157            do
158            {
159                wm->get_event(ev);
160            } while(ev.type == EV_MOUSE_MOVE && wm->IsPending());
161            wm->flush_screen();
162            if(ev.type == EV_CLOSE_WINDOW)
163                abort = 1;
164            if(ev.type == EV_KEY && ev.key == JK_ESC)
165                abort = 1;
166        } while(!abort && (ev.type != EV_MESSAGE || ev.message.id != ID_GAMMA_OK));
167
168        dg = ((spicker *)gw->inm->get(ID_GREEN_PICKER))->first_selected() * 4;
169
170        wm->close_window(gw);
171        wm->flush_screen();
172
173        wm->set_colors(wm_bc, wm_mc, wm_dc);
174        delete gray_pal;
175
176        if(!abort)
177        {
178            char *gammapath;
179            FILE *fp;
180
181            gammapath = (char *)malloc(strlen(get_save_filename_prefix()) + 10);
182            sprintf(gammapath, "%sgamma.lsp", get_save_filename_prefix());
183            fp = open_FILE(gammapath, "wb");
184            if(fp)
185            {
186                fprintf(fp, "(setq darkest_gray %ld)\n", dg);
187                fclose(fp);
188                LSpace *sp = LSpace::Current;
189                LSpace::Current = &LSpace::Perm;
190                LSymbol::FindOrCreate("darkest_gray")->SetNumber(dg);
191
192                LSpace::Current = sp;
193            }
194            else
195            {
196                dprintf("Unable to write to file gamma.lsp\n");
197            }
198            free(gammapath);
199        }
200    }
201
202    if(abort)
203        dg = old_dg;
204
205    if(dg < 1)
206        dg = 1;
207    else if(dg > 128)
208        dg = 128;
209
210    double gamma = log(dg / 255.0) / log(16.0 / 255.0);
211
212    old_pal = pal;
213    pal = new palette;
214    for(int i = 0; i < 256; i++)
215    {
216        uint8_t oldr, oldg, oldb;
217        old_pal->get(i, oldr, oldg, oldb);
218        pal->set(i, (int)(pow(oldr / 255.0, gamma) * 255),
219            (int)(pow(oldg / 255.0, gamma) * 255),
220            (int)(pow(oldb / 255.0, gamma) * 255));
221    }
222
223    pal->load();
224}
Note: See TracBrowser for help on using the repository browser.