aboutsummaryrefslogtreecommitdiff
path: root/lib/gdi/sdl.cpp
blob: eb4e2ae48e78635c35adc5a81039ba4c694b3554 (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
#include <lib/gdi/sdl.h>
#include <lib/actions/action.h>
#include <lib/base/init.h>
#include <lib/base/init_num.h>
#include <lib/driver/input_fake.h>
#include <lib/driver/rcsdl.h>

#include <SDL.h>

gSDLDC::gSDLDC() : m_pump(eApp, 1)
{
	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		eWarning("Could not initialize SDL: %s", SDL_GetError());
		return;
	}

	setResolution(720, 576);

	CONNECT(m_pump.recv_msg, gSDLDC::pumpEvent);

	m_surface.type = 0;
	m_surface.clut.colors = 256;
	m_surface.clut.data = new gRGB[m_surface.clut.colors];

	m_pixmap = new gPixmap(&m_surface);

	memset(m_surface.clut.data, 0, sizeof(*m_surface.clut.data)*m_surface.clut.colors);

	run();
}

gSDLDC::~gSDLDC()
{
	pushEvent(EV_QUIT);
	kill();
	SDL_Quit();
}

void gSDLDC::keyEvent(const SDL_Event &event)
{
	eSDLInputDriver *driver = eSDLInputDriver::getInstance();

	eDebug("SDL Key %s: key=%d", (event.type == SDL_KEYDOWN) ? "Down" : "Up", event.key.keysym.sym);

	if (driver)
		driver->keyPressed(&event.key);
}

void gSDLDC::pumpEvent(const SDL_Event &event)
{
	switch (event.type) {
	case SDL_KEYDOWN:
	case SDL_KEYUP:
		keyEvent(event);
		break;
	case SDL_QUIT:
		eDebug("SDL Quit");
		extern void quitMainloop(int exit_code);
		quitMainloop(0);
		break;
	}
}

void gSDLDC::pushEvent(enum event code, void *data1, void *data2)
{
	SDL_Event event;

	event.type = SDL_USEREVENT;
	event.user.code = code;
	event.user.data1 = data1;
	event.user.data2 = data2;

	SDL_PushEvent(&event);
}

void gSDLDC::exec(const gOpcode *o)
{
	switch (o->opcode) {
	case gOpcode::flush:
		pushEvent(EV_FLIP);
		eDebug("FLUSH");
		break;
	default:
		gDC::exec(o);
		break;
	}
}

void gSDLDC::setResolution(int xres, int yres)
{
	pushEvent(EV_SET_VIDEO_MODE, (void *)xres, (void *)yres);
}

/*
 * SDL thread below...
 */

void gSDLDC::evSetVideoMode(unsigned long xres, unsigned long yres)
{
	m_screen = SDL_SetVideoMode(xres, yres, 32, SDL_HWSURFACE);
	if (!m_screen) {
		eFatal("Could not create SDL surface: %s", SDL_GetError());
		return;
	}

	m_surface.x = m_screen->w;
	m_surface.y = m_screen->h;
	m_surface.bpp = m_screen->format->BitsPerPixel;
	m_surface.bypp = m_screen->format->BytesPerPixel;
	m_surface.stride = m_screen->pitch;
	m_surface.data = m_screen->pixels;

	SDL_EnableUNICODE(1);
}

void gSDLDC::evFlip()
{
	SDL_Flip(m_screen);
}

void gSDLDC::thread()
{
	hasStarted();

	bool stop = false;
	while (!stop) {
		SDL_Event event;
		if (SDL_WaitEvent(&event)) {
			switch (event.type) {
			case SDL_KEYDOWN:
			case SDL_KEYUP:
			case SDL_QUIT:
				m_pump.send(event);
				break;
			case SDL_USEREVENT:
				switch (event.user.code) {
				case EV_SET_VIDEO_MODE:
					evSetVideoMode((unsigned long)event.user.data1, (unsigned long)event.user.data2);
					break;
				case EV_FLIP:
					evFlip();
					break;
				case EV_QUIT:
					stop = true;
					break;
				}
				break;
			}
		}
	}
}

eAutoInitPtr<gSDLDC> init_gSDLDC(eAutoInitNumbers::graphic-1, "gSDLDC");