Rev 10 | Rev 15 | Go to most recent revision | 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 | { |
||
| 102 | obstacles.erase(obstacles.begin(), obstacles.end()); |
||
| 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 | { |
||
| 146 | snake * s; |
||
| 147 | for (int i = 0; i < snakes.size(); i++) |
||
| 148 | { |
||
| 149 | s = snakes[i]; |
||
| 150 | s.move(); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | void SSSNAKESSS::draw() |
||
| 155 | { |
||
| 156 | //drawTexture(textures["food.png"], 20, 20); |
||
| 157 | } |
||
| 158 | |||
| 159 | void SSSNAKESSS::end() |
||
| 160 | { |
||
| 161 | delete snake; |
||
| 162 | |||
| 163 | SDL_FreeSurface(screen); |
||
| 164 | SDL_Quit(); |
||
| 165 | } |
||
| 166 | |||
| 167 | vector<string> SSSNAKESSS::listDir(string dirName) |
||
| 168 | { |
||
| 169 | vector<string> files; |
||
| 170 | |||
| 171 | DIR *dp = opendir(dirName.c_str()); |
||
| 172 | if (dp != NULL) |
||
| 173 | { |
||
| 174 | dirent *ep; |
||
| 175 | while ((ep = readdir(dp))) |
||
| 176 | { |
||
| 177 | string n = ep->d_name; |
||
| 178 | if (n != "." && n != "..") |
||
| 179 | { |
||
| 180 | files.push_back(n); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | closedir(dp); |
||
| 185 | } |
||
| 186 | |||
| 187 | return files; |
||
| 188 | } |