/include/point.hpp |
---|
0,0 → 1,14 |
#ifndef POINT_HPP |
#define POINT_HPP |
class Point |
{ |
public: |
Point(); |
Point(int x, int y); |
int X; |
int Y; |
}; |
#endif // POINT_HPP |
/include/snake.hpp |
---|
0,0 → 1,35 |
#ifndef SNAKE_HPP |
#define SNAKE_HPP |
enum Direction |
{ |
Up, |
Down, |
Left, |
Right |
}; |
class Snake |
{ |
public: |
Snake(); |
void nextPoint(Point newPoint); |
void move(); |
void append(int num); |
// Stuff that should be private - Did I write this comment? (F) No (T) |
vector<Point> points; // list of points snake occupies |
// Colour Values |
int R; |
int G; |
int B; |
Direction direction; |
bool dead; |
}; |
#endif // SNAKE_HPP |
/include/sssnakesss.hpp |
---|
1,30 → 1,45 |
#ifndef SSSNAKESSS_HPP |
#define SSSNAKESSS_HPP |
struct SDL_Surface; |
class SSSNAKESSS |
{ |
public: |
SSSNAKESSS(); |
~SSSNAKESSS(); |
bool init(); |
bool initSDL(); |
bool initOpenGL(); |
void initData(); |
void run(); |
void checkEvents(); |
void update(); |
void draw(); |
void end(); |
private: |
bool running; |
SDL_Surface *screen; |
}; |
#endif // SSSNAKESSS_HPP |
#ifndef SSSNAKESSS_HPP |
#define SSSNAKESSS_HPP |
#include "point.hpp" |
class SSSNAKESSS |
{ |
public: |
SSSNAKESSS(); |
~SSSNAKESSS(); |
bool init(); |
bool initSDL(); |
bool initOpenGL(); |
void initData(); |
void run(); |
void checkEvents(); |
void update(); |
void motion(); |
void draw(); |
void initLevel(int level); |
void end(); |
vector<string> listDir(string dir); |
private: |
bool running; |
SDL_Surface *screen; |
map<string, Texture *> textures; |
Snake *snake; // controlled snake (snakes[0]) |
vector<Snake *> snakes; // all snakes |
Point food; |
vector<Point> obstacles; |
}; |
#endif // SSSNAKESSS_HPP |
/src/snake.cpp |
---|
0,0 → 1,54 |
#include <vector> |
using namespace std; |
#include "point.hpp" |
#include "snake.hpp" |
Snake::Snake() |
{ |
R = 255; |
G = 255; |
B = 0; |
direction = Up; |
dead = false; // FALSE ? |
} |
void Snake::nextPoint(Point newPoint) |
{ |
points.insert(points.begin(), newPoint); |
points.pop_back(); |
} |
void Snake::move() |
{ |
switch (direction) |
{ |
case Up: |
nextPoint(Point(points[0].X, points[0].Y - 1)); |
break; |
case Down: |
nextPoint(Point(points[0].X, points[0].Y + 1)); |
break; |
case Left: |
nextPoint(Point(points[0].X - 1, points[0].Y)); |
break; |
case Right: |
nextPoint(Point(points[0].X + 1, points[0].Y)); |
break; |
} |
} |
void Snake::append(int num) |
{ |
int index = points.size() - 1; |
if (index >= 0) |
{ |
Point p = points[index]; |
points.push_back(p); |
} |
} |
/src/point.cpp |
---|
0,0 → 1,13 |
#include "point.hpp" |
Point::Point() |
{ |
X = 0; |
Y = 0; |
} |
Point::Point(int x, int y) |
{ |
X = x; |
Y = y; |
} |
/src/sssnakesss.cpp |
---|
1,119 → 1,187 |
#include "SDL/SDL.h" |
#include "SDL/SDL_image.h" |
#include "SDL/SDL_opengl.h" |
#include <iostream> |
using namespace std; |
#include "texture.hpp" |
#include "sssnakesss.hpp" |
SSSNAKESSS::SSSNAKESSS() |
{ |
running = false; |
} |
SSSNAKESSS::~SSSNAKESSS() |
{ |
} |
bool SSSNAKESSS::init() |
{ |
if (!initSDL()) |
{ |
return false; |
} |
if (!initOpenGL()) |
{ |
return false; |
} |
initData(); |
return true; |
} |
bool SSSNAKESSS::initSDL() |
{ |
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) |
{ |
return false; |
} |
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
screen = SDL_SetVideoMode(640, 480, 16, SDL_OPENGL); |
if (!screen) |
{ |
return false; |
} |
return true; |
} |
bool SSSNAKESSS::initOpenGL() |
{ |
glEnable(GL_TEXTURE_2D); |
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
glViewport(0, 0, 640, 480); |
glClear(GL_COLOR_BUFFER_BIT); |
glMatrixMode(GL_PROJECTION); |
glLoadIdentity(); |
glOrtho(0.0f, 640, 480, 0.0f, -1.0f, 1.0f); |
glMatrixMode(GL_MODELVIEW); |
glLoadIdentity(); |
return true; |
} |
void SSSNAKESSS::initData() |
{ |
} |
void SSSNAKESSS::run() |
{ |
running = true; |
while (running) |
{ |
checkEvents(); |
update(); |
draw(); |
} |
} |
void SSSNAKESSS::checkEvents() |
{ |
SDL_Event event; |
while (SDL_PollEvent(&event)) |
{ |
switch (event.type) |
{ |
case SDL_QUIT: |
running = false; |
break; |
} |
} |
} |
void SSSNAKESSS::update() |
{ |
} |
void SSSNAKESSS::draw() |
{ |
} |
void SSSNAKESSS::end() |
{ |
} |
#include "SDL/SDL.h" |
#include "SDL/SDL_image.h" |
#include "SDL/SDL_opengl.h" |
#include <dirent.h> |
#include <iostream> |
#include <vector> |
#include <map> |
using namespace std; |
#include "point.hpp" |
#include "snake.hpp" |
#include "texture.hpp" |
#include "sssnakesss.hpp" |
SSSNAKESSS::SSSNAKESSS() |
{ |
running = false; |
} |
SSSNAKESSS::~SSSNAKESSS() |
{ |
} |
bool SSSNAKESSS::init() |
{ |
if (!initSDL()) |
{ |
return false; |
} |
if (!initOpenGL()) |
{ |
return false; |
} |
initData(); |
snake = new Snake; |
return true; |
} |
bool SSSNAKESSS::initSDL() |
{ |
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) |
{ |
return false; |
} |
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
screen = SDL_SetVideoMode(640, 480, 16, SDL_OPENGL); |
if (!screen) |
{ |
return false; |
} |
return true; |
} |
bool SSSNAKESSS::initOpenGL() |
{ |
glEnable(GL_TEXTURE_2D); |
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
glViewport(0, 0, 640, 480); |
glClear(GL_COLOR_BUFFER_BIT); |
glMatrixMode(GL_PROJECTION); |
glLoadIdentity(); |
glOrtho(0.0f, 640, 480, 0.0f, -1.0f, 1.0f); |
glMatrixMode(GL_MODELVIEW); |
glLoadIdentity(); |
return true; |
} |
void SSSNAKESSS::initData() |
{ |
string dataDir = "data/"; |
string graphicsDir = dataDir + "graphics/"; |
vector<string> graphicsList = listDir(graphicsDir); |
for (unsigned int i = 0; i < graphicsList.size(); i++) |
{ |
string file = graphicsList[i]; |
string fullfilename = graphicsDir + file; |
textures[file] = new Texture(fullfilename); |
} |
} |
void SSSNAKESSS::initLevel(int level) |
{ |
obstacles.erase(obstacles.begin(), obstacles.end() - 1); |
for (int i = 0; i < level * 3; i++) |
{ |
// add random obstacle |
} |
// reposition snakes |
} |
void SSSNAKESSS::run() |
{ |
running = true; |
while (running) |
{ |
checkEvents(); |
update(); |
draw(); |
SDL_GL_SwapBuffers(); |
} |
} |
void SSSNAKESSS::checkEvents() |
{ |
SDL_Event event; |
while (SDL_PollEvent(&event)) |
{ |
switch (event.type) |
{ |
case SDL_QUIT: |
running = false; |
break; |
} |
} |
} |
void SSSNAKESSS::update() |
{ |
motion(); |
} |
void SSSNAKESSS::motion() |
{ |
for (unsigned int i = 0; i < snakes.size(); i++) |
{ |
Snake *s = snakes[i]; |
s->move(); |
} |
} |
void SSSNAKESSS::draw() |
{ |
//drawTexture(textures["food.png"], 20, 20); |
} |
void SSSNAKESSS::end() |
{ |
delete snake; |
SDL_FreeSurface(screen); |
SDL_Quit(); |
} |
vector<string> SSSNAKESSS::listDir(string dirName) |
{ |
vector<string> files; |
DIR *dp = opendir(dirName.c_str()); |
if (dp != NULL) |
{ |
dirent *ep; |
while ((ep = readdir(dp))) |
{ |
string n = ep->d_name; |
if (n != "." && n != "..") |
{ |
files.push_back(n); |
} |
} |
closedir(dp); |
} |
return files; |
} |
/src/texture.cpp |
---|
11,7 → 11,7 |
{ |
loaded = false; |
GLenum textureFormat = GL_RGB; |
GLenum textureFormat = GL_RGBA; |
SDL_Surface *surface = IMG_Load(filename.c_str()); |
if (surface) |
/src/main.cpp |
---|
1,3 → 1,17 |
#include "SDL/SDL.h" |
#include "SDL/SDL_image.h" |
#include "SDL/SDL_opengl.h" |
#include <dirent.h> |
#include <iostream> |
#include <vector> |
#include <map> |
using namespace std; |
#include "point.hpp" |
#include "snake.hpp" |
#include "texture.hpp" |
#include "sssnakesss.hpp" |
int main(int argc, char *argv[]) |
/tmake.lua |
---|
2,7 → 2,7 |
Language("C++") |
function build() |
Set("Sources", Srcs("main.cpp", "sssnakesss.cpp", "texture.cpp")) |
Set("Sources", Srcs("main.cpp", "sssnakesss.cpp", "texture.cpp", "point.cpp", "snake.cpp")) |
Set("Libraries", Libs("SDL", "SDL_image", "SDL_mixer", "SDL_ttf", "GL")) |
Set("Includes", Incs("SDL", "SDL_image", "SDL_mixer", "SDL_ttf")) |
/history.txt |
---|
0,0 → 1,0 |
18/24/10 23:29 - Freddie: I think a point struct is needed, so I made one (sort of). Rather than building the sname properties into sssnakesss.cpp I think we should have a vector of class snake. I'm happy to write this class entirley, just want a Go-Ahead. In other news, if I am creating an instance class/struct, do you use class_name::func_name or class_name.func_name? From your code I get the impression it is the same as Static, but this seems wrong/silly. Enjoy. Oh, yeah - lets keep this going, so we know what is happening easily and can explainface stuffs. |