-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
173 lines (142 loc) · 4.27 KB
/
main.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// for initializing and shutdown functions
#include <SDL2/SDL.h>
// for rendering images and graphics on screen
#include <SDL2/SDL_image.h>
// for using SDL_Delay() functions
#include <SDL2/SDL_timer.h>
#include <iostream>
#include <vector>
#include <math.h>
#define windowWidth 640
#define windowHeight 480
#define PI 3.1415926535
float px, py, pdx = 5, pdy = 0, pa = 0; //player position pa = angle dx/dy = delta
SDL_Event e;
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
std::vector<SDL_FPoint> points;
void renderPlayer(SDL_Renderer* renderer, float px, float py)
{
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderDrawPointF(renderer, px, py);
SDL_RenderDrawLineF(renderer, px, py, (px + pdx) , (py + pdy) );
}
void playerControls(SDL_Event& event , float& px, float& py) //Basic controlls down, Fix later (there is a delay also its just an if else stement lol)
{
if (event.type == SDL_KEYDOWN)
{
const Uint8* keys = SDL_GetKeyboardState(NULL);
if (keys[SDL_SCANCODE_LEFT]) { // ROTATION
pa -= 0.1; // Rotate Left
if (pa < 0) {
pa = 2 * PI;
}
pdx = cos(pa) * 5;
pdy = sin(pa) * 5;
std::cout << "pdx = " << pdx << " and pdy = " << pdy << "and pa = " << pa << std::endl;
} else if (keys[SDL_SCANCODE_RIGHT]) {
pa += 0.1; // Rotate Right
if ( pa > (2 * PI) ) {
pa = 0;
}
pdx = cos(pa) * 5;
pdy = sin(pa) * 5;
std::cout << "pdx = " << pdx << " and pdy = " << pdy << "and pa = " << pa << std::endl;
} else if (keys[SDL_SCANCODE_W]) { //DIRECTION
py += pdy /2;
px += pdx /2;
} else if (keys[SDL_SCANCODE_S]) {
py -= pdy /2;
px -= pdx /2;
}
}
}
void renderBackground(SDL_Renderer* renderer)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
}
//temp map
int mapX= 8;
int mapY = 8;
int mapS = 32; //size
int map[] =
{
1,1,1,1,1,1,1,1,
1,0,0,1,0,0,0,1,
1,0,0,0,0,1,0,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1,
1,1,1,0,0,1,0,1,
1,0,0,0,0,1,0,1,
1,1,1,1,1,1,1,1,
};
SDL_Rect mapWallRect;
void renderMap2D(SDL_Renderer* renderer)
{
int x, y, xo, yo;
xo = 0;
yo = 0;
for (y=0; y<mapY; y++)
{
for(x=0; x<mapX; x++)
{
if(map[y*mapY+x] == 1)
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); //white
}
else
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); //black
}
xo = (x * mapS) +32; //xofset to keep on screenP
yo = (y * mapS) -32; //yofset to keep on screen
mapWallRect.x = xo - 32;
mapWallRect.y = yo + 32;
mapWallRect.w = mapS;
mapWallRect.h = mapS;
SDL_RenderDrawRect(renderer, &mapWallRect);
SDL_RenderFillRect(renderer, &mapWallRect);
}
}
}
int main()
{
// Initilization
bool running = true;
SDL_Event event;
//Window Init
SDL_Init(SDL_INIT_EVERYTHING); // SDL INIT
window = SDL_CreateWindow( "coom",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
windowWidth,
windowHeight,
0); //I dont know if I like this formatting. Saw it in a tutorial.
//Render Init
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
SDL_RenderClear(renderer);
//player position Init
px = (mapX * mapS) / 2; py = (mapY * mapS) / 2;
while(running)
{
//If you want to limit the fps you can here
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
running = false;
break;
}
playerControls(event , px , py);
}
renderBackground(renderer);
renderMap2D(renderer);
renderPlayer(renderer, px, py);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}