summaryrefslogtreecommitdiff
path: root/lib/engine.cc
blob: 6e899e211b71417dbdbe6063aaaf21f1abb4c0f4 (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
#include <SDL.h>
#include <Input.h>
#include "engine.h"
#include "glfont.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;

#define UPDATERATE 1000

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");
}

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"));
    else
	mogltk::SystemFont = new mogltk::font(&Input("font.bin"));

    return 0;
}

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

class embedRWops : public Base {
  public:
      embedRWops(Handle *);
    int seek(int, int);
    int read(void *, int, int);
    int write(const void *, int, int);
  private:
    Handle * h;
};

embedRWops::embedRWops(Handle * ah) : h(ah) {}

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);
}

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

static int embedRWread(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;
}

static int embedRWwrite(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;
}

static int embedRWclose(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) 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);
	r->seek = embedRWseek;
	r->read = embedRWread;
	r->write = embedRWwrite;
	r->close = embedRWclose;
    }
    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)
	    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 (event.key.keysym.sym == 27)
		hastoreturn = quitrequest = true;
	    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:
	    printm(M_INFO, "Mouse slept over the screen - (%i, %i)\n", event.motion.x, event.motion.y);
	    if (locked) {
		if (!((event.motion.x == mx) && (event.motion.y == my))) {
		    SDL_WarpMouse(mx, my);
		}
	    }
	    if (cursorvisible)
		hastoreturn = true;
	    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 == 4) {
		    mz--;
		}
		if (event.button.button == 5) {
		    mz++;
		}
	    }
	    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;
}

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;
}

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) {
    printm(M_INFO, "Setting up a new keyevent\n");
    keyevent_h = k;
}