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

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

ps3: make everything compile on the PS3. Of course, nothing links yet
because so much support is missing.

File size: 5.8 KB
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
[494]4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
[56]5 *
6 *  This software was released into the Public Domain. As with most public
[555]7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
[56]9 */
10
[555]11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
[56]14
[39]15#include <math.h>
16
[512]17#include "common.h"
18
[481]19#include "game.h"
[88]20
[481]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"
[2]28
29extern int dev_ok;
[110]30palette *old_pal = NULL;
[2]31
32class gray_picker : public spicker
33{
34public:
[110]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; }
[2]52
[110]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    }
[2]60};
61
[142]62static char const *lang_string(char const *symbol)
63{
[492]64    LSymbol *v = LSymbol::Find(symbol);
[484]65    if (!v || !DEFINEDP(v->GetValue()))
66        return "Language symbol missing!";
67    return lstring_value(v->GetValue());
[142]68}
[2]69
70void gamma_correct(palette *&pal, int force_menu)
71{
[110]72    long dg=0,old_dg=0;
73    int abort=0;
[2]74
[110]75    // see if user has already done this routine
[492]76    LSymbol *gs = LSymbol::Find("darkest_gray");
[2]77
[110]78    if(old_pal)
79    {
80        delete pal;
81        pal = old_pal;
82        old_pal = NULL;
83    }
[2]84
[484]85    if(gs && DEFINEDP(gs->GetValue()) && !force_menu)
[110]86    {
[484]87        dg = lnumber_value(gs->GetValue());
[110]88    }
89    else
90    {
[484]91        if(gs && DEFINEDP(gs->GetValue()))
[110]92        {
[484]93            dg = old_dg = lnumber_value(gs->GetValue());
[110]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;
[2]99
[494]100        for(; i < tc; i++)
[110]101        {
102            gray_pal->set(i, i * 4, i * 4, i * 4);
103        }
[2]104
[110]105        gray_pal->load();
[2]106
[110]107        int wm_bc = wm->bright_color(), wm_mc = wm->medium_color(), wm_dc = wm->dark_color();
[2]108
[110]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;
[2]118
[110]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;
[2]128
[110]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;
[2]138
[110]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));
[2]142
[110]143        int sh = wm->font()->height() + 35;
[123]144        button *but = new button(5, 5 + sh * 3, ID_GAMMA_OK, cache.img(ok_button),
[111]145            new info_field(35, 10 + sh * 3, ID_NULL, lang_string("gamma_msg"), 0));
[2]146
[111]147        gray_picker *gp = new gray_picker(2, 5 + sh, ID_GREEN_PICKER, 0, dg / 4, but);
[110]148        gp->set_pos(dg / 4);
[2]149
[120]150        Jwindow *gw = wm->new_window(xres / 2 - 190, yres / 2 - 90, -1, -1, gp);
[2]151
[110]152        event ev;
153        wm->flush_screen();
154        do
155        {
156            do
157            {
158                wm->get_event(ev);
159            } while(ev.type == EV_MOUSE_MOVE && wm->event_waiting());
160            wm->flush_screen();
161            if(ev.type == EV_CLOSE_WINDOW)
162                abort = 1;
163            if(ev.type == EV_KEY && ev.key == JK_ESC)
164                abort = 1;
165        } while(!abort && (ev.type != EV_MESSAGE || ev.message.id != ID_GAMMA_OK));
[2]166
[110]167        dg = ((spicker *)gw->inm->get(ID_GREEN_PICKER))->first_selected() * 4;
[2]168
[110]169        wm->close_window(gw);
170        wm->flush_screen();
[2]171
[110]172        wm->set_colors(wm_bc, wm_mc, wm_dc);
173        delete gray_pal;
[2]174
[110]175        if(!abort)
176        {
177            char *gammapath;
178            FILE *fp;
[2]179
[129]180            gammapath = (char *)malloc(strlen(get_save_filename_prefix()) + 10);
[110]181            sprintf(gammapath, "%sgamma.lsp", get_save_filename_prefix());
182            fp = open_FILE(gammapath, "wb");
183            if(fp)
184            {
185                fprintf(fp, "(setq darkest_gray %ld)\n", dg);
186                fclose(fp);
187                int sp = current_space;
188                current_space = PERM_SPACE;
[492]189                LSymbol::FindOrCreate("darkest_gray")->SetNumber(dg);
[2]190
[110]191                current_space = sp;
192            }
193            else
194            {
195                dprintf("Unable to write to file gamma.lsp\n");
196            }
[129]197            free(gammapath);
[110]198        }
199    }
[2]200
[110]201    if(abort)
202        dg = old_dg;
[2]203
[110]204    if(dg < 1)
205        dg = 1;
206    else if(dg > 128)
207        dg = 128;
[2]208
[110]209    double gamma = log(dg / 255.0) / log(16.0 / 255.0);
[2]210
[110]211    old_pal = pal;
212    pal = new palette;
213    for(int i = 0; i < 256; i++)
214    {
215        uint8_t oldr, oldg, oldb;
216        old_pal->get(i, oldr, oldg, oldb);
217        pal->set(i, (int)(pow(oldr / 255.0, gamma) * 255),
218            (int)(pow(oldg / 255.0, gamma) * 255),
219            (int)(pow(oldb / 255.0, gamma) * 255));
220    }
[2]221
[110]222    pal->load();
[2]223}
Note: See TracBrowser for help on using the repository browser.