summaryrefslogtreecommitdiff
path: root/lib/engine.cc
blob: d49574cc087c6c5c3db7db69e2a3bd14a94df143 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 *  mogltk
 *  Copyright (C) 1999-2004 Nicolas "Pixel" Noble
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* $Id: engine.cc,v 1.31 2006-10-28 16:50:46 pixel Exp $ */

#include <SDL.h>
#include <Input.h>
#include "engine.h"
#include "glfont.h"
#include "glsprite.h"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gettext.h"

bool mogltk::engine::inited = false, mogltk::engine::postsetuped = false;
bool mogltk::engine::appactive = false, mogltk::engine::cursorvisible = false, mogltk::engine::quitrequest = false;
int mogltk::engine::mx, mogltk::engine::my, mogltk::engine::mz = 0, mogltk::engine::mbuttons;
int mogltk::engine::frames, mogltk::engine::locked = 0;
double mogltk::engine::curfps = -1;
Uint32 mogltk::engine::curticks;
mogltk::Widget * mogltk::engine::root = 0;

mogltk::glbase * mogltk::engine::glbase_o = 0;
mogltk::base * mogltk::engine::base_o = 0;

mogltk::engine::keyevent * mogltk::engine::keyevent_h = 0;
mogltk::engine::mouseevent * mogltk::engine::mouseevent_h = 0;

#define UPDATERATE 1000

mogltk::engine::keyevent::keyevent() {
    new_handler = 0;
    if ((old_handler = getkeyevent()))
        old_handler->new_handler = this;
    setkeyevent(this);
}

mogltk::engine::keyevent::~keyevent() {
    if (new_handler)
        new_handler->old_handler = old_handler;
    if (old_handler)
        old_handler->new_handler = new_handler;

    if (getkeyevent() == this)
        setkeyevent(old_handler);
}

void mogltk::engine::keyevent::up(SDL_keysym) {
    printm(M_INFO, "Generic keyevent::up called\n");
}

void mogltk::engine::keyevent::down(SDL_keysym) {
    printm(M_INFO, "Generic keyevent::down called\n");
}

class keyhandler_t : public mogltk::engine::keyevent {
    virtual void down(SDL_keysym keysym) {
        if (keysym.sym == SDLK_ESCAPE)
            mogltk::engine::quit();
        else if ((keysym.sym == SDLK_RETURN) && (keysym.mod & KMOD_ALT))
            mogltk::engine::base_o->ToggleFullscreen();
        else if (old_handler)
            old_handler->down(keysym);
    }
    virtual void up(SDL_keysym keysym) {
        if (old_handler)
            old_handler->up(keysym);
    }
} basic_keyhandler;

mogltk::engine::mouseevent::mouseevent() {
    new_handler = 0;
    if ((old_handler = getmouseevent()))
        old_handler->new_handler = this;
    setmouseevent(this);
}

mogltk::engine::mouseevent::~mouseevent() {
    if (new_handler)
        new_handler->old_handler = old_handler;
    if (old_handler)
        old_handler->new_handler = new_handler;

    if (getmouseevent() == this)
        setmouseevent(old_handler);
}

void mogltk::engine::mouseevent::move(SDL_MouseMotionEvent) {
    printm(M_INFO, "Generic mouseevent::move called\n");
}

void mogltk::engine::mouseevent::action(SDL_MouseButtonEvent) {
    printm(M_INFO, "Generic mouseevent::action called\n");
}

int mogltk::engine::setup() throw(GeneralException) {
    if (inited) {
	printm(M_WARNING, FUNCNAME + _(" called twice, ignoring second call.\n"));
        return -1;
    }
    if (SDL_Init(0) < 0) {
	throw GeneralException(FUNCNAME + _(": Unable to start SDL base system"));
    }
    atexit(SDL_Quit);
    
    inited = true;
    
    return 0;
}

int mogltk::engine::postsetup() throw(GeneralException) {
    if (postsetuped) {
        printm(M_WARNING, FUNCNAME + _(" called twice, ignoring second call.\n"));
        return -1;
    }
    
    SDL_EnableUNICODE(1);
    curticks = SDL_GetTicks();
    
    postsetuped = true;

    if (glbase_o) {
	mogltk::SystemFont = new mogltk::glfont(&Input("font.bin"));
	mogltk::FixedFont = new mogltk::glfont(&Input("fixed-font.bin"));
        Sprite::Cursor = new mogltk::glSprite(&Input("cursor.rgba"), 25, 25);
    } else {
	mogltk::SystemFont = new mogltk::Font(&Input("font.bin"));
	mogltk::FixedFont = new mogltk::Font(&Input("fixed-font.bin"));
        Sprite::Cursor = new mogltk::Sprite(&Input("cursor.rgba"), 25, 25);
    }

    return 0;
}

int mogltk::engine::GetInited() {
    return inited;
}

class embedRWops : public Base {
  public:
      embedRWops(Handle *, bool);
      virtual ~embedRWops();
    int seek(int, int);
    int read(void *, int, int);
    int write(const void *, int, int);
    static int s_seek(SDL_RWops * context, int, int);
    static int s_read(SDL_RWops * context, void *, int, int);
    static int s_write(SDL_RWops * context, const void *, int, int);
    static int s_close(SDL_RWops * context);
  private:
    Handle * h;
    bool destroy_when_close;
};

embedRWops::embedRWops(Handle * ah, bool _destroy_when_close) : h(ah), destroy_when_close(_destroy_when_close) {}
embedRWops::~embedRWops() {
    if (destroy_when_close)
        delete h;
}

int embedRWops::seek(int offset, int whence) {
    return h->seek(offset, whence);
}

int embedRWops::read(void * ptr, int size, int num) {
    return h->read(ptr, size * num);
}

int embedRWops::write(const void * ptr, int size, int num) {
    return h->write(ptr, size * num);
}

int embedRWops::s_seek(SDL_RWops * context, int offset, int whence) {
    if (context->hidden.unknown.data1)
	return ((embedRWops *)(context->hidden.unknown.data1))->seek(offset, whence);
    return -1;
}

int embedRWops::s_read(SDL_RWops * context, void * ptr, int size, int num) {
    if (context->hidden.unknown.data1)
	return ((embedRWops *)(context->hidden.unknown.data1))->read(ptr, size, num);
    return -1;
}

int embedRWops::s_write(SDL_RWops * context, const void * ptr, int size, int num) {
    if (context->hidden.unknown.data1)
	return ((embedRWops *)(context->hidden.unknown.data1))->write(ptr, size, num);
    return -1;
}

int embedRWops::s_close(SDL_RWops * context) {
    if (context->hidden.unknown.data1) {
	delete ((embedRWops *)(context->hidden.unknown.data1));
	context->hidden.unknown.data1 = 0;
	return 0;
    }
    return -1;
}

SDL_RWops * mogltk::engine::RWFromHandle(Handle * h, bool destroy_when_close) throw (GeneralException) {
    SDL_RWops * r = 0;
    if (h) {
	if (!(r = SDL_AllocRW()))
	    throw GeneralException(_("Couldn't allocate memory for SDL_RWops"));
	r->hidden.unknown.data1 = (void *) new embedRWops(h, destroy_when_close);
	r->seek = embedRWops::s_seek;
	r->read = embedRWops::s_read;
	r->write = embedRWops::s_write;
	r->close = embedRWops::s_close;
    }
    return r;
}

void mogltk::engine::pollevents() throw (GeneralException) {
    SDL_Event event;
    bool hastoreturn = appactive;
    
    if (!postsetuped)
	postsetup();
    
    if (appactive) {
	Uint32 ticks = SDL_GetTicks();
	frames++;
	if (ticks - curticks > UPDATERATE) {
	    curfps = (double) frames * 1000 / (ticks - curticks);
	    frames = 0;
	    curticks = ticks;
	}
    } else {
	curfps = -1;
    }
    
    while(true) {
	if (hastoreturn || quitrequest)
	    if (!SDL_PollEvent(NULL)) {
		updatemouse();
		return;
	    }
	if (!SDL_WaitEvent(&event)) {
	    throw GeneralException("Error polling for SDL Event");
	}
	switch(event.type) {
	case SDL_ACTIVEEVENT:
	    switch (event.active.state) {
	    case SDL_APPMOUSEFOCUS:
		printm(M_INFO, String("Application ") + (event.active.gain ? "gained" : "loosed") + " mouse focus\n");
		if (cursorvisible)
		    hastoreturn = true;
		break;
	    case SDL_APPINPUTFOCUS:
		printm(M_INFO, String("Application ") + (event.active.gain ? "gained" : "loosed") + " input focus\n");
		if (cursorvisible)
		    hastoreturn = true;
		break;
	    case SDL_APPACTIVE:
		printm(M_INFO, String("Application was ") + (event.active.gain ? "restored" : "iconified"));
		if (cursorvisible)
		    hastoreturn = true;
		break;		
	    }
	    break;
	case SDL_KEYDOWN:
	case SDL_KEYUP:
	    printm(M_INFO, String("Key ") + event.key.keysym.scancode + " on keyboard " + event.key.which + (event.key.state == SDL_PRESSED ? " pressed" : " released") + "\n");
	    printm(M_INFO, "SDL keysym: %i - Unicode: %04x = `%c`- modifiers: %04x\n", event.key.keysym.sym, event.key.keysym.unicode, event.key.keysym.unicode, event.key.keysym.mod);
	    if (keyevent_h) {
		if (event.key.state == SDL_PRESSED) {
		    keyevent_h->down(event.key.keysym);
		} else {
		    keyevent_h->up(event.key.keysym);
		}
	    }
	    break;
	case SDL_MOUSEMOTION:
            if ((event.motion.x == mx) && (event.motion.y == my))
                break;
	    printm(M_INFO, "Mouse slept over the screen - (%i, %i)\n", event.motion.x, event.motion.y);
	    if (locked) {
    	        SDL_WarpMouse(mx, my);
	    }
	    if (cursorvisible)
		hastoreturn = true;
            if (mouseevent_h) {
                mouseevent_h->move(event.motion);
            }
	    break;
	case SDL_MOUSEBUTTONDOWN:
	case SDL_MOUSEBUTTONUP:
	    printm(M_INFO, String().set("Mouse button %02x ", event.button.button) + String((event.button.state == SDL_PRESSED ? "pressed" : "released")) + " at (" + event.button.x + ", " + event.button.y + ")\n");
	    if (event.button.state == SDL_PRESSED) { 
		if (event.button.button == SDL_BUTTON_WHEELUP) {
		    mz--;
		}
		if (event.button.button == SDL_BUTTON_WHEELDOWN) {
		    mz++;
		}
	    }
            if (mouseevent_h) {
                mouseevent_h->action(event.button);
            }
	    break;
	case SDL_QUIT:
	    printm(M_INFO, "Quit requested\n");
	    hastoreturn = quitrequest = true;
	    break;
	case SDL_VIDEOEXPOSE:
	    printm(M_INFO, "Needs to redraw\n");
	    hastoreturn = true;
	    break;
	default:
	    printm(M_INFO, "Unknow event: %i\n", event.type);
	    break;
	}
    }
}

void mogltk::engine::setappactive(bool p) {
    appactive = p;
}

bool mogltk::engine::getappactive() {
    return appactive;
}

void mogltk::engine::setcursorvisible(bool p) {
    cursorvisible = p;
}

bool mogltk::engine::getcursorvisible() {
    return cursorvisible;
}

void mogltk::engine::quit() {
    quitrequest = true;
}

bool mogltk::engine::quitrequested() {
    return quitrequest;
}

void mogltk::engine::updatemouse() {
    mbuttons = SDL_GetMouseState(&mx, &my);
}

int mogltk::engine::mouseX() {
    return mx;
}

int mogltk::engine::mouseY() {
    return my;
}

int mogltk::engine::mouseZ() {
    return mz;
}

void mogltk::engine::setmouseX(int _mx) {
    mx = _mx;
    SDL_WarpMouse(mx, my);
}

void mogltk::engine::setmouseY(int _my) {
    my = _my;
    SDL_WarpMouse(mx, my);
}

void mogltk::engine::setmouseZ(int _mz) {
    mz = _mz;
}

int mogltk::engine::mousebuttons() {
    return mbuttons;
}

double mogltk::engine::FPS() {
    return curfps;
}

void mogltk::engine::lockmouse() {
    locked = 1;
}

void mogltk::engine::unlockmouse() {
    locked = 0;
}

void mogltk::engine::setkeyevent(keyevent * k) {
    keyevent_h = k;
}

void mogltk::engine::setmouseevent(mouseevent * m) {
    mouseevent_h = m;
}

mogltk::engine::keyevent * mogltk::engine::getkeyevent() {
    return keyevent_h;
}

mogltk::engine::mouseevent * mogltk::engine::getmouseevent() {
    return mouseevent_h;
}