/src/sssnakesss.cpp |
---|
1,6 → 1,15 |
#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 "texture.hpp" |
#include "sssnakesss.hpp" |
SSSNAKESSS::SSSNAKESSS() |
71,6 → 80,17 |
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() |
111,3 → 131,26 |
void SSSNAKESSS::end() |
{ |
} |
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 |
---|
0,0 → 1,112 |
#include "SDL/SDL_image.h" |
#include "SDL/SDL_opengl.h" |
#include <iostream> |
using namespace std; |
#include "texture.hpp" |
Texture::Texture(string filename) |
{ |
loaded = false; |
GLenum textureFormat = GL_RGB; |
SDL_Surface *surface = IMG_Load(filename.c_str()); |
if (surface) |
{ |
GLint noOfColours = surface->format->BytesPerPixel; |
if (noOfColours == 4) |
{ |
if (surface->format->Rmask == 0x000000ff) |
{ |
textureFormat = GL_RGBA; |
} |
else |
{ |
textureFormat = GL_BGRA; |
} |
} |
else if (noOfColours == 3) |
{ |
if (surface->format->Rmask == 0x000000ff) |
{ |
textureFormat = GL_RGB; |
} |
else |
{ |
textureFormat = GL_BGR; |
} |
} |
else |
{ |
// Big Problem! |
} |
glGenTextures(1, &texture); |
glBindTexture(GL_TEXTURE_2D, texture); |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
width = surface->w; |
height = surface->h; |
halfWidth = width * 0.5; |
halfHeight = height * 0.5; |
glTexImage2D(GL_TEXTURE_2D, 0, noOfColours, width, height, 0, textureFormat, GL_UNSIGNED_BYTE, surface->pixels); |
SDL_FreeSurface(surface); |
loaded = true; |
} |
} |
Texture::~Texture() |
{ |
if (loaded) |
{ |
glDeleteTextures(1, &texture); |
} |
} |
void Texture::draw(int x, int y) |
{ |
if (loaded) |
{ |
glBindTexture(GL_TEXTURE_2D, texture); |
int x1 = x - halfWidth; |
int x2 = x + halfWidth; |
int y1 = y - halfHeight; |
int y2 = y + halfHeight; |
glBegin(GL_QUADS); |
//Bottom-left vertex (corner) |
glTexCoord2i(0, 0); |
glVertex3f(x1, y2, 0.0f); |
//Bottom-right vertex (corner) |
glTexCoord2i(1, 0); |
glVertex3f(x2, y2, 0.f); |
//Top-right vertex (corner) |
glTexCoord2i(1, 1); |
glVertex3f(x2, y1, 0.f); |
//Top-left vertex (corner) |
glTexCoord2i(0, 1); |
glVertex3f(x1, y1, 0.f); |
glEnd(); |
} |
} |
void drawTexture(Texture *texture, int x, int y) |
{ |
if (texture) |
{ |
texture->draw(x, y); |
} |
} |
/include/sssnakesss.hpp |
---|
1,5 → 1,5 |
#ifndef SSSNAKESSS_H |
#define SSSNAKESSS_H |
#ifndef SSSNAKESSS_HPP |
#define SSSNAKESSS_HPP |
struct SDL_Surface; |
20,11 → 20,15 |
void draw(); |
void end(); |
vector<string> listDir(string dir); |
private: |
bool running; |
SDL_Surface *screen; |
map<string, Texture *> textures; |
}; |
#endif // SSSNAKESSS_H |
#endif // SSSNAKESSS_HPP |
/include/texture.hpp |
---|
0,0 → 1,24 |
#ifndef TEXTURE_HPP |
#define TEXTURE_HPP |
class Texture |
{ |
public: |
Texture(string filename); |
~Texture(); |
void draw(int x, int y); |
private: |
GLuint texture; |
bool loaded; |
int width; |
int height; |
int halfWidth; |
int halfHeight; |
}; |
void drawTexture(Texture *texture, int x, int y); |
#endif // TEXTURE_HPP |
/tmake.lua |
---|
2,7 → 2,7 |
Language("C++") |
function build() |
Set("Sources", Srcs("main.cpp", "sssnakesss.cpp")) |
Set("Sources", Srcs("main.cpp", "sssnakesss.cpp", "texture.cpp")) |
Set("Libraries", Libs("SDL", "SDL_image", "SDL_mixer", "SDL_ttf", "GL")) |
Set("Includes", Incs("SDL", "SDL_image", "SDL_mixer", "SDL_ttf")) |