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

Last change on this file since 494 was 494, checked in by Sam Hocevar, 11 years ago

style: remove trailing spaces, fix copyright statements.

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