-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
147 lines (128 loc) · 4.54 KB
/
main.cpp
File metadata and controls
147 lines (128 loc) · 4.54 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
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
#include <algorithm>
#include <iostream>
#include "SDL3/SDL.h"
#include "SDL3/SDL_main.h"
int main(int argc, char *argv[])
{
SDL_Window *window = nullptr;
SDL_Renderer *renderer = nullptr;
SDL_Surface *imageSurface = nullptr;
SDL_Texture *texture = nullptr;
SDL_Event event;
std::cout << "Launching..." << std::endl;
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0){
std::cout << "Couldn't initialize SDL: " << SDL_GetError() << std::endl;
return -1;
}
if (SDL_CreateWindowAndRenderer("It's Slime Time", 640, 480, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE|SDL_WINDOW_INPUT_FOCUS, &window, &renderer) != 0) {
std::cout << "Couldn't create window and renderer: " << SDL_GetError() << std::endl;
return -2;
}
imageSurface = SDL_LoadBMP("C:\\Users\\Jeff\\dev\\assets\\slime.bmp");
if (imageSurface == NULL) {
std::cout << "Couldn't create surface from image: " << SDL_GetError() << std::endl;
return -3;
}
texture = SDL_CreateTextureFromSurface(renderer, imageSurface);
if (texture == NULL) {
std::cout << "Couldn't create texture from surface: " << SDL_GetError() << std::endl;
return -4;
}
int textureWidth,textureHeight;
SDL_FRect textureRect;
SDL_QueryTexture(texture, NULL, NULL, &textureWidth, &textureHeight);
textureRect.x=0;
textureRect.y=0;
textureRect.w=textureWidth;
textureRect.h=textureHeight;
SDL_DestroySurface(imageSurface);
SDL_Surface *windowSurface = SDL_GetWindowSurface(window);
bool running {true};
const auto StepSize {10};
bool up, down, left, right;
up = down = left = right = false;
while (running)
{
if(SDL_PollEvent(&event)==SDL_TRUE)
{
switch(event.type)
{
case SDL_EVENT_QUIT:
running = false;
break;
case SDL_EVENT_KEY_DOWN:
switch(event.key.keysym.sym)
{
case SDLK_UP: //fall-through
case SDLK_w:
up = true;
break;
case SDLK_DOWN: //fall-through
case SDLK_s:
down = true;
break;
case SDLK_LEFT: //fall-through
case SDLK_a:
left = true;
break;
case SDLK_RIGHT: //fall-through
case SDLK_d:
right = true;
break;
}
break;
case SDL_EVENT_KEY_UP:
switch(event.key.keysym.sym)
{
case SDLK_UP: //fall-through
case SDLK_w:
up = false;
break;
case SDLK_DOWN: //fall-through
case SDLK_s:
down = false;
break;
case SDLK_LEFT: //fall-through
case SDLK_a:
left = false;
break;
case SDLK_RIGHT: //fall-through
case SDLK_d:
right = false;
break;
}
break;
}
}
if(up)
{
textureRect.y -= StepSize;
}
if(down)
{
textureRect.y += StepSize;
}
if(left)
{
textureRect.x -= StepSize;
}
if(right)
{
textureRect.x += StepSize;
}
// keep texture within window
textureRect.y = std::clamp(static_cast<int>(textureRect.y),0,static_cast<int>(windowSurface->h - textureRect.h));
textureRect.x = std::clamp(static_cast<int>(textureRect.x),0,static_cast<int>(windowSurface->w - textureRect.w));
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(renderer);
SDL_RenderTexture(renderer, texture, NULL, &textureRect);
SDL_RenderPresent(renderer);
}
std::cout << "Exiting..." << std::endl;
SDL_DestroySurface(windowSurface);
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}