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

Last change on this file since 484 was 484, checked in by Sam Hocevar, 12 years ago

lisp: populate LispSymbol? with symbol-related methods.

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