source: abuse/branches/lol/src/gamma.cpp @ 732

Last change on this file since 732 was 732, checked in by Sam Hocevar, 8 years ago

build: SDL2 compilation fixes.

File size: 5.4 KB
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
[715]4 *  Copyright (c) 2005-2013 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
[732]11#if HAVE_CONFIG_H
[555]12#   include "config.h"
13#endif
[56]14
[39]15#include <math.h>
16
[512]17#include "common.h"
18
[724]19#include "lisp/lisp.h"
20
21#include "imlib/window.h"
22#include "imlib/scroller.h"
23#include "imlib/dprint.h"
24
[481]25#include "game.h"
26#include "id.h"
27#include "cache.h"
28#include "loader2.h"
[2]29
30extern int dev_ok;
[694]31Palette *old_pal = NULL;
[2]32
[715]33class AGrayPicker : public AScrollPicker
[2]34{
35public:
[715]36    AGrayPicker(ivec2 pos, int id, int start, int current)
37      : AScrollPicker(pos, id, 1, 20, 0, 0)
38    {
39        cur_sel = current;
40        sc = start;
41        reconfigure();
42        cur_sel = current;
43    }
44
[110]45    int sc;
[715]46    virtual void DrawItem(AImage *screen, ivec2 pos, int num, int active)
[110]47    {
[715]48        ivec2 corner = pos + ivec2(item_width() - 1, item_height() - 1);
49        screen->Bar(pos, corner, 0);
50        screen->Bar(pos, corner - ivec2(3, 0), sc + num);
[110]51        if(active)
52        {
[715]53            screen->Rectangle(pos, corner, 255);
[110]54        }
55    }
56    void set_pos(int x) { cur_sel = x; }
57    virtual int total() { return 32; }
58    virtual int item_width() { return 12; }
59    virtual int item_height() { return 20; }
60    virtual int activate_on_mouse_move() { return 0; }
[2]61};
62
[142]63static char const *lang_string(char const *symbol)
64{
[492]65    LSymbol *v = LSymbol::Find(symbol);
[484]66    if (!v || !DEFINEDP(v->GetValue()))
67        return "Language symbol missing!";
68    return lstring_value(v->GetValue());
[142]69}
[2]70
[694]71void gamma_correct(Palette *&pal, int force_menu)
[2]72{
[110]73    long dg=0,old_dg=0;
74    int abort=0;
[2]75
[110]76    // see if user has already done this routine
[492]77    LSymbol *gs = LSymbol::Find("darkest_gray");
[2]78
[110]79    if(old_pal)
80    {
81        delete pal;
82        pal = old_pal;
[694]83        old_pal = nullptr;
[110]84    }
[2]85
[484]86    if(gs && DEFINEDP(gs->GetValue()) && !force_menu)
[110]87    {
[484]88        dg = lnumber_value(gs->GetValue());
[110]89    }
90    else
91    {
[484]92        if(gs && DEFINEDP(gs->GetValue()))
[110]93        {
[484]94            dg = old_dg = lnumber_value(gs->GetValue());
[110]95        }
96        // load in a fine gray palette they can chose from
[694]97        Palette *gray_pal = pal->Copy();
[110]98        int tc = 32;
[2]99
[694]100        for(int i = 0; i < tc; i++)
101            gray_pal->SetColor(i, u8vec3(i * 4));
[2]102
[708]103        gray_pal->Load();
[2]104
[110]105        int wm_bc = wm->bright_color(), wm_mc = wm->medium_color(), wm_dc = wm->dark_color();
[2]106
[694]107        u8vec3 br = (u8vec3)min((ivec3)pal->GetColor(wm_bc), ivec3(255 - 20)) + u8vec3(20);
108        u8vec3 md = (u8vec3)max((ivec3)pal->GetColor(wm_mc), ivec3(20)) - u8vec3(20);
109        u8vec3 dr = (u8vec3)max((ivec3)pal->GetColor(wm_dc), ivec3(40)) - u8vec3(40);
[2]110
[694]111        wm->set_colors(gray_pal->FindClosest(br),
112                       gray_pal->FindClosest(md),
113                       gray_pal->FindClosest(dr));
[2]114
[668]115        int sh = wm->font()->Size().y + 35;
[2]116
[715]117        AWidgetList widgets;
118        widgets << new AButton(ivec2(5, 5 + sh * 3), ID_GAMMA_OK, cache.img(ok_button));
119        widgets << new AInfoField(ivec2(35, 10 + sh * 3), ID_NULL, lang_string("gamma_msg"));
120
121        AGrayPicker *gp = new AGrayPicker(ivec2(2, 5 + sh), ID_GREEN_PICKER, 0, dg / 4);
[110]122        gp->set_pos(dg / 4);
[715]123        widgets << gp;
[2]124
[715]125        AWindow *gw = wm->CreateWindow(ivec2(xres / 2 - 190,
126                                             yres / 2 - 90), ivec2(-1), "", widgets);
[2]127
[643]128        Event ev;
[110]129        wm->flush_screen();
130        do
131        {
132            do
133            {
134                wm->get_event(ev);
[646]135            } while(ev.type == EV_MOUSE_MOVE && wm->IsPending());
[110]136            wm->flush_screen();
137            if(ev.type == EV_CLOSE_WINDOW)
138                abort = 1;
139            if(ev.type == EV_KEY && ev.key == JK_ESC)
140                abort = 1;
141        } while(!abort && (ev.type != EV_MESSAGE || ev.message.id != ID_GAMMA_OK));
[2]142
[715]143        dg = ((AScrollPicker *)gw->inm->get(ID_GREEN_PICKER))->first_selected() * 4;
[2]144
[110]145        wm->close_window(gw);
146        wm->flush_screen();
[2]147
[110]148        wm->set_colors(wm_bc, wm_mc, wm_dc);
149        delete gray_pal;
[2]150
[110]151        if(!abort)
152        {
153            char *gammapath;
154            FILE *fp;
[2]155
[129]156            gammapath = (char *)malloc(strlen(get_save_filename_prefix()) + 10);
[110]157            sprintf(gammapath, "%sgamma.lsp", get_save_filename_prefix());
158            fp = open_FILE(gammapath, "wb");
159            if(fp)
160            {
161                fprintf(fp, "(setq darkest_gray %ld)\n", dg);
162                fclose(fp);
[635]163                LSpace *sp = LSpace::Current;
164                LSpace::Current = &LSpace::Perm;
[492]165                LSymbol::FindOrCreate("darkest_gray")->SetNumber(dg);
[2]166
[635]167                LSpace::Current = sp;
[110]168            }
169            else
170            {
171                dprintf("Unable to write to file gamma.lsp\n");
172            }
[129]173            free(gammapath);
[110]174        }
175    }
[2]176
[110]177    if(abort)
178        dg = old_dg;
[2]179
[110]180    if(dg < 1)
181        dg = 1;
182    else if(dg > 128)
183        dg = 128;
[2]184
[110]185    double gamma = log(dg / 255.0) / log(16.0 / 255.0);
[2]186
[110]187    old_pal = pal;
[694]188    pal = new Palette;
[110]189    for(int i = 0; i < 256; i++)
190    {
[694]191        u8vec3 old = old_pal->GetColor(i);
192        pal->SetColor(i, u8vec3(lol::pow(old.r / 255.0, gamma) * 255,
193                                lol::pow(old.g / 255.0, gamma) * 255,
194                                lol::pow(old.b / 255.0, gamma) * 255));
[110]195    }
[2]196
[708]197    pal->Load();
[2]198}
Note: See TracBrowser for help on using the repository browser.