forked from miguelinux/ogl-game-tut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamewindow.cpp
123 lines (92 loc) · 3.42 KB
/
gamewindow.cpp
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
#include "gamewindow.h"
#define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_PNG
#include <stb_image.h>
typedef struct {
GLfloat positionCoordinates[3];
GLfloat textureCoordinates[2];
} VertexData;
VertexData vertices[] = {
{{0.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
{{SQUARE_SIZE, 0.0f, 0.0f}, {1.0f, 1.0f}},
{{SQUARE_SIZE, SQUARE_SIZE, 0.0f}, {1.0f, 0.0f}},
{{0.0f, SQUARE_SIZE, 0.0f}, {0.0f, 0.0f}}
};
void GameWindow::setRunning(bool newRunning)
{
_running = newRunning;
}
bool GameWindow::getRunning()
{
return _running;
}
GLuint GameWindow::loadAndBufferImage(const char *filename)
{
int width, height, bpp /* 8-bit component per pixel */;
unsigned char *data = NULL;
GLuint textureBufferID;
data = stbi_load(filename, &width, &height, &bpp, 0);
if (data == NULL)
return 0;
glGenTextures(1, &textureBufferID);
glBindTexture(GL_TEXTURE_2D, textureBufferID);
/* glTexImage2D - specify a two-dimensional texture image
* void glTexImage2D(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format,
* GLenum type, const GLvoid * data);
*/
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
stbi_image_free(data);
return textureBufferID;
}
GameWindow::GameWindow(bool running, GLFWwindow* window): _running(running),
_vertexBufferID(0)
{
Vector2 rocketPosition;
_window = window;
/* glClearColor - specify clear values for the color buffers
void glClearColor(GLfloat red, GLfloat green, GLfloat blue,
GLfloat alpha); */
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
/* This function retrieves the size, in pixels, of the framebuffer of
* the specified window. */
glfwGetFramebufferSize(window, &_width, &_height);
glViewport(0, 0, _width, _height);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* void glOrtho(GLdouble left, GLdouble right, GLdouble bottom,
GLdouble top, GLdouble nearVal, GLdouble farVal); */
glOrtho(0.0, _width, 0.0, _height, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glGenBuffers(1, &_vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferID);
/* creates and initializes a buffer object's data store */
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(VertexData), (GLvoid *) offsetof(VertexData,positionCoordinates));
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(VertexData), (GLvoid *) offsetof(VertexData, textureCoordinates));
_textureBufferID = loadAndBufferImage("nave.png");
rocketPosition.x = 300;
rocketPosition.y = 200;
_rocket = new PlayerSprite(window, _textureBufferID, rocketPosition);
_rocket->setBoundingBox(makeBoundingBox(_height, 0, 0, _width));
/* _rocket->setVelocity(makeVector2(2.0f, 2.0f)); */
}
void GameWindow::render()
{
glClear(GL_COLOR_BUFFER_BIT);
_rocket->render();
/* Swap front and back buffers */
glfwSwapBuffers(_window);
}
void GameWindow::update()
{
_rocket->update();
}