1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
#ifndef _MAIN_WINDOW_H
#define _MAIN_WINDOW_H
#ifdef __APPLE__
#ifdef APPLE_NATIVE_GLUT
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
#include <GL/gl.h>
#include <GL/glut.h>
#endif
#else
#include <GL/freeglut.h>
#endif
#include "types.h"
#include <string>
#include <vector>
#include "Player.h"
#include "Cell.h"
#include "Field.h"
#include "Game.h"
#include "KeyboardController.h"
enum TextAlign {
right = -2,
center = -1,
left = 0
};
// Functions defines for C callbacks
/**
* Called when the window is resized.
* May not work yet on all platforms.
**/
void f_onChangeSize(int w, int h);
/**
* Called when timer finishes.
**/
void f_onTimer(int v=0);
/**
* Called when we want to redisplay (Idle)
**/
void f_onDisplay();
/**
* Called when an ASCII key is pressed.
**/
void f_onNormalKeyPressed(unsigned char key, int x, int y);
/**
* Called when a special key is pressed.
**/
void f_onSpecialKeyPressed(int key, int x, int y);
/**
* Transforms an hexadecimal color into its 3 channels, as floats (0.f - 1.f)
**/
void hex2fColor(uint color, float &out_red, float &out_green, float &out_blue);
/**
* Singleton : display window
**/
class MainWindow
{
private :
MainWindow(); // constructor is private,
MainWindow( MainWindow const & ); // copy constructor is private
MainWindow &operator=( MainWindow const & ); // Assignment operator
virtual ~MainWindow(); // ... and destructor.
public:
/**
* Retrieves (create if necessary) the single instance of the class.
**/
static MainWindow &getInstance();
public:
/**
* Initializes freeglut (glutinit)
**/
void init(int *argcp, char **argv);
/**
* Defines window size and name, creates it and defines proper projection matrix
**/
void create(std::string name, int x, int y, int width, int height);
/**
* Registers callbacks and start glut's infinite loop.
**/
void startLoop();
/**
* Sets the Field we want to draw from
**/
void setField(Field* pField);
/**
* Returns the field we draw from.
**/
Field* getField();
/**
* Sets the game we will periodically update
**/
void setGame(Game* pGame);
/**
* Returns the game we will periodically update
**/
Game* getGame();
/**
* Dynamically allocate a new KeyboardController, register it and return it.
**/
KeyboardController* generateNewKeyboardController(Player *pPlayer, KeyCode leftKey, KeyCode rightKey);
public: // Callbacks
/**
* Is called when window size is changed
**/
void onChangeSize(int w, int h);
/**
* Is called when display is redrawn
**/
void onRenderScene();
/**
* is called every (X) ms.
**/
void onTimer();
/**
* Is called when a special key is pressed
**/
void onSpecialKeyPressed(int key);
/**
* is called when an ASCII key is pressed
**/
void onNormalKeyPressed(uint8 key);
private:
void onKeyPressed(KeyCode key);
void drawCell(uint Color);
void drawString(GLfloat x, GLfloat y, char *text, uint color, TextAlign align);
private:
static MainWindow s_mainWindow;
Field *m_pField;
Game *m_pGame;
int m_width, m_height;
std::vector<KeyboardController*> m_vpKeyboardController;
};
#endif
|