summaryrefslogtreecommitdiff
path: root/lib/engine.cc
blob: d065346e00512ea49e662c8d890bcef1581762fa (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
#include "engine.h"

#define _(x) x

int mogltk::engine::inited = 0;

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

    inited = 1;

    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() {
    SDL_Event event;
    
    while(true) {
	SDL_WaitEvent(&event);
	switch(event.type) {
	case SDL_VIDEOEXPOSE:
	    printm(M_INFO, "Needs to redraw\n");
	    return;
	case SDL_MOUSEMOTION:
	    printm(M_INFO, "Mouse has gone over the screen - (%i, %i)\n", event.motion.x, event.motion.y);
	    break;
	case SDL_QUIT:
	    printm(M_INFO, "Quit requested\n");
	    exit(0);
	    break;
	case SDL_ACTIVEEVENT:
	    switch (event.active.state) {
	    case SDL_APPMOUSEFOCUS:
		printm(M_INFO, String("Application ") + (event.active.gain ? "gained" : "loosed") + " mouse focus\n");
		break;
	    case SDL_APPINPUTFOCUS:
		printm(M_INFO, String("Application ") + (event.active.gain ? "gained" : "loosed") + " input focus\n");
		break;
	    case SDL_APPACTIVE:
		printm(M_INFO, String("Application was ") + (event.active.gain ? "restored" : "iconified"));
		break;		
	    }
	    break;
	default:
	    printm(M_INFO, "Unknow event: %i\n", event.type);
	    break;
	}
    }
}