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

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

ps3: compilation fixes for non-PS3 platforms.

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(x, y, x2, y2, 0);
41        screen->bar(x, y, x2 - 3, y2, sc + num);
42        if(active)
43        {
44            screen->rectangle(x, y, 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()->height() + 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->new_window(xres / 2 - 190, yres / 2 - 90, -1, -1, gp);
151
152        Event ev;
153        wm->flush_screen();
154#if !defined __CELLOS_LV2__
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#else
170        /* Do not display the gamma window on the PS3 for now. */
171#endif
172
173        wm->close_window(gw);
174        wm->flush_screen();
175
176        wm->set_colors(wm_bc, wm_mc, wm_dc);
177        delete gray_pal;
178
179        if(!abort)
180        {
181            char *gammapath;
182            FILE *fp;
183
184            gammapath = (char *)malloc(strlen(get_save_filename_prefix()) + 10);
185            sprintf(gammapath, "%sgamma.lsp", get_save_filename_prefix());
186            fp = open_FILE(gammapath, "wb");
187            if(fp)
188            {
189                fprintf(fp, "(setq darkest_gray %ld)\n", dg);
190                fclose(fp);
191                LSpace *sp = LSpace::Current;
192                LSpace::Current = &LSpace::Perm;
193                LSymbol::FindOrCreate("darkest_gray")->SetNumber(dg);
194
195                LSpace::Current = sp;
196            }
197            else
198            {
199                dprintf("Unable to write to file gamma.lsp\n");
200            }
201            free(gammapath);
202        }
203    }
204
205    if(abort)
206        dg = old_dg;
207
208    if(dg < 1)
209        dg = 1;
210    else if(dg > 128)
211        dg = 128;
212
213    double gamma = log(dg / 255.0) / log(16.0 / 255.0);
214
215    old_pal = pal;
216    pal = new palette;
217    for(int i = 0; i < 256; i++)
218    {
219        uint8_t oldr, oldg, oldb;
220        old_pal->get(i, oldr, oldg, oldb);
221        pal->set(i, (int)(pow(oldr / 255.0, gamma) * 255),
222            (int)(pow(oldg / 255.0, gamma) * 255),
223            (int)(pow(oldb / 255.0, gamma) * 255));
224    }
225
226    pal->load();
227}
Note: See TracBrowser for help on using the repository browser.