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

Last change on this file since 112 was 111, checked in by Sam Hocevar, 11 years ago
  • Simplified the window creation arguments. Got rid of a lot of macros and hid stuff in private namespaces.

Inspired by Win32 Abuse changelog for January 28, 2001:

  • Well, in the process of adding changes necessary to

handle recovery from alt-tabbing away from Abuse
(which is why I was updating jwindow::redraw()),
the entire windowing system is getting an overhaul.
It's gonna be sweet when I'm done, though.

  • jwindow::redraw() has been changed to a virtual

function requiring no parameters. This'll make
it much easier to implement special specific-
purpose windows.

File size: 5.5 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 sh = wm->font()->height() + 35;
[111]133        button *but = new button(5, 5 + sh * 3, ID_GAMMA_OK, cash.img(ok_button),
134            new info_field(35, 10 + sh * 3, ID_NULL, lang_string("gamma_msg"), 0));
[2]135
[111]136        gray_picker *gp = new gray_picker(2, 5 + sh, ID_GREEN_PICKER, 0, dg / 4, but);
[110]137        gp->set_pos(dg / 4);
[2]138
[110]139        jwindow *gw = wm->new_window(xres / 2 - 190, yres / 2 - 90, -1, -1, gp);
[2]140
[110]141        event ev;
142        wm->flush_screen();
143        do
144        {
145            do
146            {
147                wm->get_event(ev);
148            } while(ev.type == EV_MOUSE_MOVE && wm->event_waiting());
149            wm->flush_screen();
150            if(ev.type == EV_CLOSE_WINDOW)
151                abort = 1;
152            if(ev.type == EV_KEY && ev.key == JK_ESC)
153                abort = 1;
154        } while(!abort && (ev.type != EV_MESSAGE || ev.message.id != ID_GAMMA_OK));
[2]155
[110]156        dg = ((spicker *)gw->inm->get(ID_GREEN_PICKER))->first_selected() * 4;
[2]157
[110]158        wm->close_window(gw);
159        wm->flush_screen();
[2]160
[110]161        wm->set_colors(wm_bc, wm_mc, wm_dc);
162        delete gray_pal;
[2]163
[110]164        if(!abort)
165        {
166            char *gammapath;
167            FILE *fp;
[2]168
[110]169            gammapath = (char *)jmalloc(strlen(get_save_filename_prefix()) + 10, "gammapath");
170            sprintf(gammapath, "%sgamma.lsp", get_save_filename_prefix());
171            fp = open_FILE(gammapath, "wb");
172            if(fp)
173            {
174                fprintf(fp, "(setq darkest_gray %ld)\n", dg);
175                fclose(fp);
176                int sp = current_space;
177                current_space = PERM_SPACE;
178                set_symbol_value(make_find_symbol("darkest_gray"), new_lisp_number(dg));
[2]179
[110]180                current_space = sp;
181            }
182            else
183            {
184                dprintf("Unable to write to file gamma.lsp\n");
185            }
186            jfree(gammapath);
187        }
188    }
[2]189
[110]190    if(abort)
191        dg = old_dg;
[2]192
[110]193    if(dg < 1)
194        dg = 1;
195    else if(dg > 128)
196        dg = 128;
[2]197
[110]198    double gamma = log(dg / 255.0) / log(16.0 / 255.0);
[2]199
[110]200    old_pal = pal;
201    pal = new palette;
202    for(int i = 0; i < 256; i++)
203    {
204        uint8_t oldr, oldg, oldb;
205        old_pal->get(i, oldr, oldg, oldb);
206        pal->set(i, (int)(pow(oldr / 255.0, gamma) * 255),
207            (int)(pow(oldg / 255.0, gamma) * 255),
208            (int)(pow(oldb / 255.0, gamma) * 255));
209    }
[2]210
[110]211    pal->load();
[2]212}
Note: See TracBrowser for help on using the repository browser.