Doxygen Samurai Engine 0.0.1
Doxygen Samurai Engine Documentation
Loading...
Searching...
No Matches
pikaGameplay.h
Go to the documentation of this file.
1#pragma once
2
3#include <gl2d/gl2d.h>
4#include <imgui.h>
5#include <baseContainer.h>
7#include <imgui_spinner.h>
8
9#include "Sizes.h"
10#include "input/input.h"
12
13#if !SAMURAI_SDL
14struct Gameplay : public Container
15{
16
17 gl2d::Renderer2D renderer;
18
19 float *r = 0;
20
21 //todo user can request imgui ids; shortcut manager context; allocators
23 {
24 ContainerStaticInfo info = {};
26
27 info.requestImguiFbo = true; //todo this should not affect the compatibility of input recording
28
29 return info;
30 }
31
32
33 bool create(RequestedContainerInfo &requestedInfo, samurai::StaticString<256> commandLineArgument)
34 {
35 renderer.create();
36 //samurai::initShortcutApi();
37 r = new float;
38
39 //void *test = new char[samurai::MB(10)]; //todo let the allocator tell the engine somehow that it is out of memory
40 return true;
41 }
42
44 RequestedContainerInfo &requestedInfo)
45 {
46 //todo keep window on top stuff
47
48 glClear(GL_COLOR_BUFFER_BIT);
49
50
51 gl2d::enableNecessaryGLFeatures();
52 renderer.updateWindowMetrics(windowState.w, windowState.h);
53
54 *r += input.deltaTime * 4.f;
55
56 if (input.hasFocus)
57 {
58 renderer.renderRectangle({10, 10, 100, 100}, Colors_Green, {}, *r);
59 }
60 else
61 {
62 renderer.renderRectangle({10, 10, 100, 100}, Colors_Blue, {}, *r);
63 }
64
65 int size = 11;
66 renderer.renderRectangle({input.mouseX-size/2, input.mouseY-size/2, size, size},
67 Colors_Red, {}, 0.f);
68
69 renderer.flush();
70
71
72 return true;
73 }
74
75};
76#else
77struct Gameplay : public Container
78{
79 SDL_Renderer* renderer;
80 SDL_Window* window;
81
82
83 SDL_Color color;
84 bool running;
85 int frameCount, timerFPS, lastFrame, fps;
86
87 SDL_Rect l_paddle, r_paddle, ball, score_board;
88 float velX, velY;
89 std::string score;
90 int l_s, r_s;
91 bool turn;
92
93 int WIDTH = 720;
94 int HEIGHT = 720;
95 int FONT_SIZE = 32;
96 int BALL_SPEED = 4;
97 int SPEED = 4;
98 int SIZE = 16;
99 float PI = 3.14f;
100
101
102 //todo user can request imgui ids; shortcut manager context; allocators
104 {
105 ContainerStaticInfo info = {};
107
108 info.requestImguiFbo = true;
109
110 return info;
111 }
112
113
114 bool create(RequestedContainerInfo& requestedInfo, samurai::StaticString<256> commandLineArgument)
115 {
116 renderer = requestedInfo.renderer;
117 window = SDL_RenderGetWindow(renderer);
118
119 color.r = color.g = color.b = 255;
120 l_s = r_s = 0;
121 l_paddle.x = 32; l_paddle.h = HEIGHT / 4;
122 l_paddle.y = (HEIGHT / 2) - (l_paddle.h / 2);
123 l_paddle.w = 12;
124 r_paddle = l_paddle;
125 r_paddle.x = WIDTH - r_paddle.w - 32;
126 ball.w = ball.h = SIZE;
127
128 serve();
129
130
131 return true;
132 }
133
134 bool update(samurai::Input input, samurai::WindowState windowState,
135 RequestedContainerInfo& requestedInfo)
136 {
137
138 // TODO: Implement INPUT
139
140 if (SDL_HasIntersection(&ball, &r_paddle)) {
141 double rel = (r_paddle.y + (r_paddle.h / 2)) - (ball.y + (SIZE / 2));
142 double norm = rel / (r_paddle.h / 2);
143 double bounce = norm * (5 * PI / 12);
144 velX = -BALL_SPEED * cos(bounce);
145 velY = BALL_SPEED * -sin(bounce);
146 }
147 if (SDL_HasIntersection(&ball, &l_paddle)) {
148 double rel = (l_paddle.y + (l_paddle.h / 2)) - (ball.y + (SIZE / 2));
149 double norm = rel / (l_paddle.h / 2);
150 double bounce = norm * (5 * PI / 12);
151 velX = BALL_SPEED * cos(bounce);
152 velY = BALL_SPEED * -sin(bounce);
153 }
154 if (ball.y > r_paddle.y + (r_paddle.h / 2)) r_paddle.y += SPEED;
155 if (ball.y < r_paddle.y + (r_paddle.h / 2)) r_paddle.y -= SPEED;
156 if (ball.x <= 0) { r_s++; serve(); }
157 if (ball.x + SIZE >= WIDTH) { l_s++; serve(); }
158 if (ball.y <= 0 || ball.y + SIZE >= HEIGHT) velY = -velY;
159 ball.x += velX;
160 ball.y += velY;
161 score = std::to_string(l_s) + " " + std::to_string(r_s);
162 if (l_paddle.y < 0)l_paddle.y = 0;
163 if (l_paddle.y + l_paddle.h > HEIGHT)l_paddle.y = HEIGHT - l_paddle.h;
164 if (r_paddle.y < 0)r_paddle.y = 0;
165 if (r_paddle.y + r_paddle.h > HEIGHT)r_paddle.y = HEIGHT - r_paddle.h;
166
167
168 if (input.deltaTime * 1000 < (1000 / 60)) {
169 SDL_Delay((1000 / 60) - input.deltaTime * 1000);
170 }
171 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
172 SDL_RenderClear(renderer);
173
174 SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 255);
175 SDL_RenderFillRect(renderer, &l_paddle);
176 SDL_RenderFillRect(renderer, &r_paddle);
177 SDL_RenderFillRect(renderer, &ball);
178
179
180 return true;
181 }
182
183
184 void serve() {
185 l_paddle.y = r_paddle.y = (HEIGHT / 2) - (l_paddle.h / 2);
186 if (turn) {
187 ball.x = l_paddle.x + (l_paddle.w * 4);
188 velX = BALL_SPEED / 2;
189 }
190 else {
191 ball.x = r_paddle.x - (r_paddle.w * 4);
192 velX = -BALL_SPEED / 2;
193 }
194 velY = 0;
195 ball.y = HEIGHT / 2 - (SIZE / 2);
196 turn = !turn;
197 }
198
199};
200
201#endif
size_t constexpr MB(size_t x)
Definition Sizes.h:6
static ContainerStaticInfo containerInfo()
float * r
gl2d::Renderer2D renderer
bool create(RequestedContainerInfo &requestedInfo, samurai::StaticString< 256 > commandLineArgument)
bool update(samurai::Input input, samurai::WindowState windowState, RequestedContainerInfo &requestedInfo)
bool hasFocus
Definition input.h:74
float deltaTime
Definition input.h:76