Этот шаблон позволяет быстро создать приложение SDL1.2 (то есть окно) в операционной системе Linux.
§ main.cc
1#include <stdio.h>
2#include <sdlapp.cc>
3
4class MainApp : public SDLApp {
5
6public:
7
8
9 MainApp(int w, int h, const char* caption) : SDLApp(w, h, caption) { }
10
11
12 int Event(SDL_Event e) { return 1; };
13};
14
15int main(int argc, char* argv[]) {
16
17 MainApp* app = new MainApp(1024, 768, "Caption");
18 app->main();
19 return 0;
20}
§ sdlapp.cc
1#include "SDL.h"
2
3
4uint DisplayTimer(uint interval, void *param) {
5
6 SDL_Event event;
7 SDL_UserEvent userevent;
8
9
10 userevent.type = SDL_USEREVENT;
11 userevent.code = 0;
12 userevent.data1 = NULL;
13 userevent.data2 = NULL;
14
15 event.type = SDL_USEREVENT;
16 event.user = userevent;
17
18 SDL_PushEvent(& event);
19 return (interval);
20}
21
22class SDLApp {
23
24protected:
25
26 int width, height;
27 SDL_Event event;
28 SDL_Surface* sdl_screen;
29
30
31 void pset(int x, int y, uint color) {
32
33 if (x >= 0 && y >= 0 && x < width && y < height) {
34 ( (Uint32*)sdl_screen->pixels )[ x + width*y ] = color;
35 }
36 }
37
38
39 void flip() {
40 SDL_Flip(sdl_screen);
41 }
42
43
44 int get_key(SDL_Event event) {
45
46 SDL_KeyboardEvent * eventkey = & event.key;
47 return eventkey->keysym.scancode;
48 }
49
50public:
51
52 virtual int Event(SDL_Event) = 0;
53
54 SDLApp(int w, int h, const char* caption) {
55
56 width = w;
57 height = h;
58
59 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
60 SDL_EnableUNICODE(1);
61
62 sdl_screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
63 SDL_WM_SetCaption(caption, 0);
64
65
66 SDL_AddTimer(20, DisplayTimer, NULL);
67 }
68
69
70 void main() {
71
72 while (1) {
73
74 while (SDL_PollEvent(& event)) {
75
76
77 switch (event.type) {
78
79
80 case SDL_QUIT: return;
81 default: Event(event);
82 }
83 }
84
85 SDL_Delay(1);
86 }
87 }
88};
§ makefile
CC=g++
SDL=`sdl-config --cflags --libs`
LIBS=-lSDL
WARN=-Wall
CFLAGS=-I. $(SDL) -O3 $(WARN)
OBJ=main.o
all: $(OBJ)
$(CC) $(CFLAGS) $(OBJ) $(LIBS) -o main
./main
%.o: %.cc
$(CC) $(CFLAGS) $(LIBS) $< -c -o $@
clean:
rm *.o