This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
PongMode.hpp
74 lines (53 loc) · 2.2 KB
/
PongMode.hpp
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
#include "ColorTextureProgram.hpp"
#include "Mode.hpp"
#include "GL.hpp"
#include <vector>
#include <deque>
/*
* PongMode is a game mode that implements a single-player game of Pong.
*/
struct PongMode : Mode {
PongMode();
virtual ~PongMode();
//functions called by main loop:
virtual bool handle_event(SDL_Event const &, glm::uvec2 const &window_size) override;
virtual void update(float elapsed) override;
virtual void draw(glm::uvec2 const &drawable_size) override;
//----- game state -----
glm::vec2 court_radius = glm::vec2(7.0f, 5.0f);
glm::vec2 paddle_radius = glm::vec2(0.2f, 1.0f);
glm::vec2 ball_radius = glm::vec2(0.2f, 0.2f);
glm::vec2 left_paddle = glm::vec2(-court_radius.x + 0.5f, 0.0f);
glm::vec2 right_paddle = glm::vec2( court_radius.x - 0.5f, 0.0f);
glm::vec2 ball = glm::vec2(0.0f, 0.0f);
glm::vec2 ball_velocity = glm::vec2(-1.0f, 0.0f);
uint32_t left_score = 0;
uint32_t right_score = 0;
float ai_offset = 0.0f;
float ai_offset_update = 0.0f;
//----- pretty rainbow trails -----
float trail_length = 1.3f;
std::deque< glm::vec3 > ball_trail; //stores (x,y,age), oldest elements first
//----- opengl assets / helpers ------
//draw functions will work on vectors of vertices, defined as follows:
struct Vertex {
Vertex(glm::vec3 const &Position_, glm::u8vec4 const &Color_, glm::vec2 const &TexCoord_) :
Position(Position_), Color(Color_), TexCoord(TexCoord_) { }
glm::vec3 Position;
glm::u8vec4 Color;
glm::vec2 TexCoord;
};
static_assert(sizeof(Vertex) == 4*3 + 1*4 + 4*2, "PongMode::Vertex should be packed");
//Shader program that draws transformed, vertices tinted with vertex colors:
ColorTextureProgram color_texture_program;
//Buffer used to hold vertex data during drawing:
GLuint vertex_buffer = 0;
//Vertex Array Object that maps buffer locations to color_texture_program attribute locations:
GLuint vertex_buffer_for_color_texture_program = 0;
//Solid white texture:
GLuint white_tex = 0;
//matrix that maps from clip coordinates to court-space coordinates:
glm::mat3x2 clip_to_court = glm::mat3x2(1.0f);
// computed in draw() as the inverse of OBJECT_TO_CLIP
// (stored here so that the mouse handling code can use it to position the paddle)
};