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

Last change on this file since 106 was 106, checked in by Sam Hocevar, 15 years ago
  • Rename the "eh" variable to "wm" because it's a window manager, not an event handler.
  • No longer pass the window manager to functions, there's only one.

Inspired by Win32 Abuse changelog for January 28, 2001:

  • Starting work on singleton code; will get rid of all

references to an arbitrary window_manager* because
there's only going to be one, and it's not ever
going to change.

File size: 4.9 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.hpp"
15
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;
26palette *old_pal=NULL;
27
28class gray_picker : public spicker
29{
30public:
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; }
48
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        }
56};
57
58
59void gamma_correct(palette *&pal, int force_menu)
60{
61        long dg=0,old_dg=0;
62        int abort=0;
63
64        // see if user has already done this routine
65        Cell *gs = find_symbol("darkest_gray");
66
67        if( old_pal )
68        {
69                delete pal;
70                pal = old_pal;
71                old_pal = NULL;
72        }
73
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;
88
89                for( ;i < tc; i++ )
90                {
91                        gray_pal->set( i, i * 4, i * 4, i * 4 );
92                }
93
94                gray_pal->load();
95
96                int wm_bc = wm->bright_color(), wm_mc = wm->medium_color(), wm_dc = wm->dark_color();
97
98                int br_r = pal->red( wm_bc ) + 20;
99                if( br_r > 255)
100                {
101                        br_r = 255;
102                }
103                int br_g = pal->green( wm_bc ) + 20;
104                if( br_g > 255 )
105                {
106                        br_g = 255;
107                }
108                int br_b = pal->blue( wm_bc ) + 20;
109                if( br_b > 255 )
110                {
111                        br_b = 255;
112                }
113
114                int md_r = pal->red( wm_mc ) - 20;
115                if( md_r < 0 )
116                {
117                        md_r = 0;
118                }
119                int md_g = pal->green( wm_mc ) - 20;
120                if( md_g < 0 )
121                {
122                        md_g = 0;
123                }
124                int md_b = pal->blue( wm_mc ) - 20;
125                if( md_b < 0 )
126                {
127                        md_b = 0;
128                }
129
130                int dr_r = pal->red( wm_dc ) - 40;
131                if( dr_r < 0 )
132                {
133                        dr_r = 0;
134                }
135                int dr_g = pal->green( wm_dc ) - 40;
136                if( dr_g < 0 )
137                {
138                        dr_g = 0;
139                }
140                int dr_b = pal->blue( wm_dc ) - 40;
141                if( dr_b < 0 )
142                {
143                        dr_b = 0;
144                }
145
146                wm->set_colors( gray_pal->find_closest( br_r, br_g, br_b ),
147                        gray_pal->find_closest( md_r, md_g, md_b ),
148                        gray_pal->find_closest( dr_r, dr_g, dr_b ) );
149
150                int wl = WINDOW_FRAME_LEFT, wh = WINDOW_FRAME_TOP;
151                int sh = wm->font()->height() + 35;
152                button *but = new button( wl + 5, wh + 5 + sh * 3, ID_GAMMA_OK, cash.img( ok_button ),
153                        new info_field( wl + 35, wh + 10 + sh * 3, ID_NULL, lang_string( "gamma_msg" ), 0 ) );
154
155                gray_picker *gp = new gray_picker( wl + 2, wh + 5 + sh * 1, ID_GREEN_PICKER, 0, dg / 4, but);
156                gp->set_pos( dg / 4 );
157
158                jwindow *gw = wm->new_window( xres / 2 - 190, yres / 2 - 90, -1, -1, gp);
159
160                event ev;
161                wm->flush_screen();
162                do
163                {
164                        do
165                        {
166                                wm->get_event(ev);
167                        } while( ev.type == EV_MOUSE_MOVE && wm->event_waiting() );
168                        wm->flush_screen();
169                        if( ev.type == EV_CLOSE_WINDOW)
170                        {
171                                abort = 1;
172                        }
173                        if( ev.type == EV_KEY && ev.key == JK_ESC)
174                        {
175                                abort = 1;
176                        }
177                } while( !abort && ( ev.type != EV_MESSAGE || ev.message.id != ID_GAMMA_OK ) );
178
179                dg = ( (spicker *)gw->inm->get( ID_GREEN_PICKER ) )->first_selected() * 4;
180
181                wm->close_window( gw );
182                wm->flush_screen();
183
184                wm->set_colors( wm_bc, wm_mc, wm_dc);
185                delete gray_pal;
186
187                if( !abort )
188                {
189                        char *gammapath;
190                        FILE *fp;
191
192                        gammapath = (char *)jmalloc( strlen( get_save_filename_prefix() ) + 10, "gammapath" );
193                        sprintf( gammapath, "%sgamma.lsp", get_save_filename_prefix() );
194                        fp = open_FILE( gammapath, "wb" );
195                        if( fp )
196                        {
197                                fprintf( fp, "(setq darkest_gray %ld)\n", dg );
198                                fclose(fp);
199                                int sp = current_space;
200                                current_space = PERM_SPACE;
201                                set_symbol_value( make_find_symbol("darkest_gray"), new_lisp_number( dg ) );
202
203                                current_space = sp;
204                        }
205                        else
206                        {
207                                dprintf( "Unable to write to file gamma.lsp\n" );
208                        }
209                        jfree( gammapath);
210                }
211        }
212
213        if( abort )
214        {
215                dg = old_dg;
216        }
217
218        if( dg < 1 )
219        {
220                dg = 1;
221        }
222        else if( dg > 128 )
223        {
224                dg = 128;
225        }
226
227        double gamma = log( dg / 255.0 ) / log( 16.0 / 255.0 );
228
229        old_pal = pal;
230        pal = new palette;
231        for( int i = 0; i < 256; i++ )
232        {
233                uint8_t oldr, oldg, oldb;
234                old_pal->get( i, oldr, oldg, oldb );
235                pal->set( i, (int)( pow( oldr / 255.0, gamma ) * 255 ),
236                        (int)( pow( oldg / 255.0, gamma ) * 255 ),
237                        (int)( pow( oldb / 255.0, gamma ) * 255 ) );
238        }
239
240        pal->load();
241}
Note: See TracBrowser for help on using the repository browser.