-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
61 lines (50 loc) · 1.61 KB
/
main.cpp
File metadata and controls
61 lines (50 loc) · 1.61 KB
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
#include <SDL2/SDL.h>
#include <emscripten/emscripten.h>
#include <iostream>
// GLOBAL APP STATE
struct AppState {
SDL_Window* window;
SDL_Renderer* renderer;
int box_x = 100;
int box_y = 100;
int velocity = 2;
};
AppState app;
// ==========================================
// YOUR LOGIC GOES HERE (The Loop)
// ==========================================
void main_loop() {
// 1. HANDLE EVENTS (Taps/Clicks)
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_MOUSEBUTTONDOWN) {
// Reverse direction on tap
app.velocity *= -1;
std::cout << "Tap detected! Velocity is now " << app.velocity << std::endl;
}
}
// 2. UPDATE STATE
app.box_x += app.velocity;
// Bounce off walls (assuming 800px width)
if (app.box_x > 750 || app.box_x < 0) app.velocity *= -1;
// 3. RENDER (Draw the Screen)
// Clear screen to Black
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 255);
SDL_RenderClear(app.renderer);
// Draw Red Box
SDL_Rect box = { app.box_x, app.box_y, 50, 50 }; // x, y, width, height
SDL_SetRenderDrawColor(app.renderer, 255, 0, 0, 255);
SDL_RenderFillRect(app.renderer, &box);
// Show it
SDL_RenderPresent(app.renderer);
}
// ==========================================
// SETUP (Do Not Touch)
// ==========================================
int main() {
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(800, 600, 0, &app.window, &app.renderer);
// Tell the browser to run "main_loop" infinitely
emscripten_set_main_loop(main_loop, 0, 1);
return 0;
}