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