Subversion Repositories sssnakesss

Rev

Rev 8 | 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
 
10 tom 12
#include "point.hpp"
13
#include "snake.hpp"
4 tom 14
#include "texture.hpp"
1 tom 15
#include "sssnakesss.hpp"
16
 
17
SSSNAKESSS::SSSNAKESSS()
18
{
2 tom 19
    running = false;
1 tom 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
 
10 tom 41
    snake = new Snake;
42
 
1 tom 43
    return true;
44
}
45
 
46
bool SSSNAKESSS::initSDL()
47
{
3 tom 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
 
1 tom 61
    return true;
62
}
63
 
64
bool SSSNAKESSS::initOpenGL()
65
{
3 tom 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
 
1 tom 82
    return true;
83
}
84
 
85
void SSSNAKESSS::initData()
86
{
6 tom 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
    }
1 tom 98
}
99
 
100
void SSSNAKESSS::run()
101
{
2 tom 102
    running = true;
103
 
104
    while (running)
105
    {
106
        checkEvents();
107
        update();
108
        draw();
8 tom 109
 
110
        SDL_GL_SwapBuffers();
2 tom 111
    }
1 tom 112
}
113
 
2 tom 114
void SSSNAKESSS::checkEvents()
115
{
3 tom 116
    SDL_Event event;
117
 
118
    while (SDL_PollEvent(&event))
119
    {
120
        switch (event.type)
121
        {
122
            case SDL_QUIT:
123
                running = false;
124
                break;
125
        }
126
    }
2 tom 127
}
128
 
129
void SSSNAKESSS::update()
130
{
131
}
132
 
133
void SSSNAKESSS::draw()
134
{
10 tom 135
    //drawTexture(textures["food.png"], 20, 20);
2 tom 136
}
137
 
1 tom 138
void SSSNAKESSS::end()
139
{
10 tom 140
    delete snake;
141
 
8 tom 142
    SDL_FreeSurface(screen);
143
    SDL_Quit();
1 tom 144
}
6 tom 145
 
7 tom 146
vector<string> SSSNAKESSS::listDir(string dirName)
6 tom 147
{
7 tom 148
    vector<string> files;
6 tom 149
 
7 tom 150
    DIR *dp = opendir(dirName.c_str());
151
    if (dp != NULL)
152
    {
153
        dirent *ep;
154
        while ((ep = readdir(dp)))
155
        {
156
            string n = ep->d_name;
157
            if (n != "." && n != "..")
158
            {
159
                files.push_back(n);
160
            }
161
        }
162
 
163
        closedir(dp);
164
    }
165
 
166
    return files;
6 tom 167
}