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

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