Rev 6 | Rev 8 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3 | tom | 1 | #include "SDL/SDL.h" |
4 | tom | 2 | #include "SDL/SDL_image.h" |
3 | tom | 3 | #include "SDL/SDL_opengl.h" |
4 | |||
7 | tom | 5 | #include <dirent.h> |
4 | tom | 6 | #include <iostream> |
6 | tom | 7 | #include <vector> |
8 | #include <map> |
||
4 | tom | 9 | |
10 | using namespace std; |
||
11 | |||
12 | #include "texture.hpp" |
||
1 | tom | 13 | #include "sssnakesss.hpp" |
14 | |||
15 | SSSNAKESSS::SSSNAKESSS() |
||
16 | { |
||
2 | tom | 17 | running = false; |
1 | tom | 18 | } |
19 | |||
20 | SSSNAKESSS::~SSSNAKESSS() |
||
21 | { |
||
22 | |||
23 | } |
||
24 | |||
25 | bool SSSNAKESSS::init() |
||
26 | { |
||
27 | if (!initSDL()) |
||
28 | { |
||
29 | return false; |
||
30 | } |
||
31 | |||
32 | if (!initOpenGL()) |
||
33 | { |
||
34 | return false; |
||
35 | } |
||
36 | |||
37 | initData(); |
||
38 | |||
39 | return true; |
||
40 | } |
||
41 | |||
42 | bool SSSNAKESSS::initSDL() |
||
43 | { |
||
3 | tom | 44 | if (SDL_Init(SDL_INIT_EVERYTHING) != 0) |
45 | { |
||
46 | return false; |
||
47 | } |
||
48 | |||
49 | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
||
50 | |||
51 | screen = SDL_SetVideoMode(640, 480, 16, SDL_OPENGL); |
||
52 | if (!screen) |
||
53 | { |
||
54 | return false; |
||
55 | } |
||
56 | |||
1 | tom | 57 | return true; |
58 | } |
||
59 | |||
60 | bool SSSNAKESSS::initOpenGL() |
||
61 | { |
||
3 | tom | 62 | glEnable(GL_TEXTURE_2D); |
63 | |||
64 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
||
65 | |||
66 | glViewport(0, 0, 640, 480); |
||
67 | |||
68 | glClear(GL_COLOR_BUFFER_BIT); |
||
69 | |||
70 | glMatrixMode(GL_PROJECTION); |
||
71 | glLoadIdentity(); |
||
72 | |||
73 | glOrtho(0.0f, 640, 480, 0.0f, -1.0f, 1.0f); |
||
74 | |||
75 | glMatrixMode(GL_MODELVIEW); |
||
76 | glLoadIdentity(); |
||
77 | |||
1 | tom | 78 | return true; |
79 | } |
||
80 | |||
81 | void SSSNAKESSS::initData() |
||
82 | { |
||
6 | tom | 83 | string dataDir = "data/"; |
84 | |||
85 | string graphicsDir = dataDir + "graphics/"; |
||
86 | vector<string> graphicsList = listDir(graphicsDir); |
||
87 | for (unsigned int i = 0; i < graphicsList.size(); i++) |
||
88 | { |
||
89 | string file = graphicsList[i]; |
||
90 | string fullfilename = graphicsDir + file; |
||
91 | |||
92 | textures[file] = new Texture(fullfilename); |
||
93 | } |
||
1 | tom | 94 | } |
95 | |||
96 | void SSSNAKESSS::run() |
||
97 | { |
||
2 | tom | 98 | running = true; |
99 | |||
100 | while (running) |
||
101 | { |
||
102 | checkEvents(); |
||
103 | update(); |
||
104 | draw(); |
||
105 | } |
||
1 | tom | 106 | } |
107 | |||
2 | tom | 108 | void SSSNAKESSS::checkEvents() |
109 | { |
||
3 | tom | 110 | SDL_Event event; |
111 | |||
112 | while (SDL_PollEvent(&event)) |
||
113 | { |
||
114 | switch (event.type) |
||
115 | { |
||
116 | case SDL_QUIT: |
||
117 | running = false; |
||
118 | break; |
||
119 | } |
||
120 | } |
||
2 | tom | 121 | } |
122 | |||
123 | void SSSNAKESSS::update() |
||
124 | { |
||
125 | } |
||
126 | |||
127 | void SSSNAKESSS::draw() |
||
128 | { |
||
129 | } |
||
130 | |||
1 | tom | 131 | void SSSNAKESSS::end() |
132 | { |
||
133 | } |
||
6 | tom | 134 | |
7 | tom | 135 | vector<string> SSSNAKESSS::listDir(string dirName) |
6 | tom | 136 | { |
7 | tom | 137 | vector<string> files; |
6 | tom | 138 | |
7 | tom | 139 | DIR *dp = opendir(dirName.c_str()); |
140 | if (dp != NULL) |
||
141 | { |
||
142 | dirent *ep; |
||
143 | while ((ep = readdir(dp))) |
||
144 | { |
||
145 | string n = ep->d_name; |
||
146 | if (n != "." && n != "..") |
||
147 | { |
||
148 | files.push_back(n); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | closedir(dp); |
||
153 | } |
||
154 | |||
155 | return files; |
||
6 | tom | 156 | } |