-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
91 lines (75 loc) · 2.42 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
#include <iostream>
#include <SDL.h>
#include "Entity.h"
int main(int argc, char* args[])
{
SDL_Init(SDL_INIT_VIDEO);
int SCREEN_WIDTH = 640;
int SCREEN_HEIGHT = 480;
int displayIndex = 0;
SDL_Rect displayBounds;
SDL_GetDisplayBounds(displayIndex, &displayBounds);
SCREEN_HEIGHT = displayBounds.h;
SCREEN_WIDTH = displayBounds.w;
SDL_Window* window = SDL_CreateWindow("Player Movement", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
bool IsOpen = true;
Uint32 lastTime = SDL_GetTicks();
float deltaTime;
Entity player(renderer);
float playerSpeed = 4.f;
float playerMaxSpeed = 4.f;
while (IsOpen)
{
SDL_Event event;
Uint32 currentTime = SDL_GetTicks();
deltaTime = (currentTime - lastTime) / 1000.0f;
lastTime = currentTime;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
IsOpen = false;
break;
}
}
const Uint8* state = SDL_GetKeyboardState(NULL);
SDL_Point move = { 0, 0 };
if (state[SDL_SCANCODE_A])
{
move.x -= (100 * playerSpeed) * deltaTime;
}
if (state[SDL_SCANCODE_D])
{
move.x += (100 * playerSpeed) * deltaTime;
}
if (state[SDL_SCANCODE_W])
{
move.y -= (100 * playerSpeed) * deltaTime;
}
if (state[SDL_SCANCODE_S])
{
move.y += (100 * playerSpeed) * deltaTime;
}
if (playerMaxSpeed < move.y)
move.y = playerMaxSpeed;
else if (-playerMaxSpeed > move.y)
move.y = -playerMaxSpeed;
if (playerMaxSpeed < move.x)
move.x = playerMaxSpeed;
else if (-playerMaxSpeed > move.x)
move.x = -playerMaxSpeed;
player.Move(move);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
player.Update();
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}