Skip to content

Commit 790fde9

Browse files
committed
Moved SDL elements out of global scope
1 parent 0f854fb commit 790fde9

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

include/Actor.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#include "Actor.hpp"
22
#include <iostream>
33
#include <array>
4-
using namespace std;
54

6-
Actor::Actor(array<float, 2> pPos, array<float, 2> pVelocity, array<float, 2> pDesiredVelocity, float pMass, float pRadius) {
5+
Actor::Actor(std::array<float, 2> pPos, std::array<float, 2> pVelocity, std::array<float, 2> pDesiredVelocity, float pMass, float pRadius) {
76
pos = pPos;
87
velocity = pVelocity;
98
desiredVelocity = pDesiredVelocity;

include/Actor.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33

44
#include<iostream>
55
#include <array>
6-
using namespace std;
76

87
class Actor {
98
private:
10-
array<float, 2> pos;
11-
array<float, 2> velocity;
12-
array<float, 2> desiredVelocity;
9+
std::array<float, 2> pos;
10+
std::array<float, 2> velocity;
11+
std::array<float, 2> desiredVelocity;
1312
float mass;
1413
float radius;
1514
public:
16-
Actor(array<float, 2> pPos, array<float, 2> pVelocity, array<float, 2> pDesiredVelocity, float pMass, float pRadius);
15+
Actor(std::array<float, 2> pPos, std::array<float, 2> pVelocity, std::array<float, 2> pDesiredVelocity, float pMass, float pRadius);
1716
};
1817

1918
#endif

src/main.cpp

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,56 @@
22
#include <SDL2/SDL.h>
33
#include "Actor.hpp"
44
#include <iostream>
5-
using namespace std;
65

7-
SDL_Window* win = NULL;
8-
SDL_Surface* surface = NULL;
9-
SDL_Renderer* render = NULL;
6+
const int WIDTH = 8; // metres
7+
const int HEIGHT = 6; // metres
8+
const int SCALE = 100;
109

11-
const int WIDTH = 800;
12-
const int HEIGHT = 600;
13-
14-
void init() {
10+
void init(SDL_Window* win, SDL_Renderer* render, std::vector<Actor> &actors) {
1511
SDL_Init(SDL_INIT_VIDEO);
16-
win = SDL_CreateWindow("SYCL Crowd Simulation", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
12+
win = SDL_CreateWindow("SYCL Crowd Simulation", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH * SCALE, HEIGHT * SCALE, SDL_WINDOW_SHOWN);
1713
render = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
14+
15+
actors.push_back(Actor{{1,2}, {3,4}, {5,6}, 3, 4});
1816
}
1917

20-
void draw() {
18+
void drawCircle(SDL_Renderer* render, SDL_Point center, int radius, SDL_Color color) {
19+
SDL_SetRenderDrawColor(render, color.r, color.g, color.b, color.a);
20+
for (int w = 0; w < radius * 2; w++) {
21+
for (int h = 0; h < radius * 2; h++) {
22+
int dx = radius - w;
23+
int dy = radius - h;
24+
if ((dx*dx + dy*dy) <= (radius * radius)) {
25+
SDL_RenderDrawPoint(render, center.x + dx, center.y + dy);
26+
}
27+
}
28+
}
29+
}
30+
31+
void draw(SDL_Renderer* render, std::vector<Actor> &actors) {
2132
SDL_SetRenderDrawColor(render, 255, 255, 255, 255);
2233
SDL_RenderClear(render);
2334

35+
36+
2437
SDL_RenderPresent(render);
2538
}
2639

27-
void close() {
40+
void close(SDL_Window* win) {
2841
SDL_DestroyWindow(win);
2942
SDL_Quit();
3043
}
3144

3245
int main() {
33-
init();
46+
SDL_Window* win = NULL;
47+
SDL_Surface* surface = NULL;
48+
SDL_Renderer* render = NULL;
49+
50+
std::vector<Actor> actors;
51+
52+
init(win, render, actors);
3453

35-
draw();
54+
draw(render, actors);
3655

3756
Actor test = Actor({0, 2}, {3, 4}, {5,6}, 12, 3);
3857

@@ -46,9 +65,9 @@ int main() {
4665
}
4766
}
4867

49-
draw();
68+
draw(render, actors);
5069
}
5170

52-
close();
71+
close(win);
5372
return 0;
5473
}

0 commit comments

Comments
 (0)