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

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

Fuck the history, I'm renaming all .hpp files to .h for my own sanity.

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  void *v=find_symbol(symbol);
60  if (!v || !DEFINEDP(symbol_value(v))) return "Language symbol missing!";
61  else return lstring_value(symbol_value(v));
62}
63
64void gamma_correct(palette *&pal, int force_menu)
65{
66    long dg=0,old_dg=0;
67    int abort=0;
68
69    // see if user has already done this routine
70    Cell *gs = find_symbol("darkest_gray");
71
72    if(old_pal)
73    {
74        delete pal;
75        pal = old_pal;
76        old_pal = NULL;
77    }
78
79    if(gs && DEFINEDP(symbol_value(gs)) && !force_menu)
80    {
81        dg = lnumber_value(symbol_value(gs));
82    }
83    else
84    {
85        if(gs && DEFINEDP(symbol_value(gs)))
86        {
87            dg = old_dg = lnumber_value(symbol_value(gs));
88        }
89        // load in a fine gray palette they can chose from
90        palette *gray_pal = pal->copy();
91        int i = 0;
92        int tc = 32;
93
94        for(;i < tc; i++)
95        {
96            gray_pal->set(i, i * 4, i * 4, i * 4);
97        }
98
99        gray_pal->load();
100
101        int wm_bc = wm->bright_color(), wm_mc = wm->medium_color(), wm_dc = wm->dark_color();
102
103        int br_r = pal->red(wm_bc) + 20;
104        if(br_r > 255)
105            br_r = 255;
106        int br_g = pal->green(wm_bc) + 20;
107        if(br_g > 255)
108            br_g = 255;
109        int br_b = pal->blue(wm_bc) + 20;
110        if(br_b > 255)
111            br_b = 255;
112
113        int md_r = pal->red(wm_mc) - 20;
114        if(md_r < 0)
115            md_r = 0;
116        int md_g = pal->green(wm_mc) - 20;
117        if(md_g < 0)
118            md_g = 0;
119        int md_b = pal->blue(wm_mc) - 20;
120        if(md_b < 0)
121            md_b = 0;
122
123        int dr_r = pal->red(wm_dc) - 40;
124        if(dr_r < 0)
125            dr_r = 0;
126        int dr_g = pal->green(wm_dc) - 40;
127        if(dr_g < 0)
128            dr_g = 0;
129        int dr_b = pal->blue(wm_dc) - 40;
130        if(dr_b < 0)
131            dr_b = 0;
132
133        wm->set_colors(gray_pal->find_closest(br_r, br_g, br_b),
134            gray_pal->find_closest(md_r, md_g, md_b),
135            gray_pal->find_closest(dr_r, dr_g, dr_b));
136
137        int sh = wm->font()->height() + 35;
138        button *but = new button(5, 5 + sh * 3, ID_GAMMA_OK, cache.img(ok_button),
139            new info_field(35, 10 + sh * 3, ID_NULL, lang_string("gamma_msg"), 0));
140
141        gray_picker *gp = new gray_picker(2, 5 + sh, ID_GREEN_PICKER, 0, dg / 4, but);
142        gp->set_pos(dg / 4);
143
144        Jwindow *gw = wm->new_window(xres / 2 - 190, yres / 2 - 90, -1, -1, gp);
145
146        event ev;
147        wm->flush_screen();
148        do
149        {
150            do
151            {
152                wm->get_event(ev);
153            } while(ev.type == EV_MOUSE_MOVE && wm->event_waiting());
154            wm->flush_screen();
155            if(ev.type == EV_CLOSE_WINDOW)
156                abort = 1;
157            if(ev.type == EV_KEY && ev.key == JK_ESC)
158                abort = 1;
159        } while(!abort && (ev.type != EV_MESSAGE || ev.message.id != ID_GAMMA_OK));
160
161        dg = ((spicker *)gw->inm->get(ID_GREEN_PICKER))->first_selected() * 4;
162
163        wm->close_window(gw);
164        wm->flush_screen();
165
166        wm->set_colors(wm_bc, wm_mc, wm_dc);
167        delete gray_pal;
168
169        if(!abort)
170        {
171            char *gammapath;
172            FILE *fp;
173
174            gammapath = (char *)malloc(strlen(get_save_filename_prefix()) + 10);
175            sprintf(gammapath, "%sgamma.lsp", get_save_filename_prefix());
176            fp = open_FILE(gammapath, "wb");
177            if(fp)
178            {
179                fprintf(fp, "(setq darkest_gray %ld)\n", dg);
180                fclose(fp);
181                int sp = current_space;
182                current_space = PERM_SPACE;
183                set_symbol_value(make_find_symbol("darkest_gray"), new_lisp_number(dg));
184
185                current_space = sp;
186            }
187            else
188            {
189                dprintf("Unable to write to file gamma.lsp\n");
190            }
191            free(gammapath);
192        }
193    }
194
195    if(abort)
196        dg = old_dg;
197
198    if(dg < 1)
199        dg = 1;
200    else if(dg > 128)
201        dg = 128;
202
203    double gamma = log(dg / 255.0) / log(16.0 / 255.0);
204
205    old_pal = pal;
206    pal = new palette;
207    for(int i = 0; i < 256; i++)
208    {
209        uint8_t oldr, oldg, oldb;
210        old_pal->get(i, oldr, oldg, oldb);
211        pal->set(i, (int)(pow(oldr / 255.0, gamma) * 255),
212            (int)(pow(oldg / 255.0, gamma) * 255),
213            (int)(pow(oldb / 255.0, gamma) * 255));
214    }
215
216    pal->load();
217}
Note: See TracBrowser for help on using the repository browser.