Subversion Repositories sssnakesss

Compare Revisions

Ignore whitespace Rev 13 → Rev 14

/src/sssnakesss.cpp
1,167 → 1,188
#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::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()
{
}
 
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;
}
#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());
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()
{
snake * s;
for (int i = 0; i < snakes.size(); i++)
{
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/snake.cpp
18,3 → 18,18
points.insert(points.begin(), newPoint);
points.pop_back();
}
 
void Snake::move()
{
switch (direction)
{
case up:
nextPoint(Point(points[0].X, points[0].Y - 1));
case down:
nextPoint(Point(points[0].X, points[0].Y + 1));
case left:
nextPoint(Point(points[0].X - 1, points[0].Y));
case right:
nextPoint(Point(points[0].X + 1, points[0].Y));
}
}