Subversion Repositories sssnakesss

Rev

Rev 7 | Rev 14 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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