-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
77 lines (68 loc) · 2.42 KB
/
Copy pathgame.cpp
File metadata and controls
77 lines (68 loc) · 2.42 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
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
#include "game.h"
const int SCREEN_WIDTH = 1400;
const int SCREEN_HEIGHT = 800;
const std::string WINDOW_TITLE = "Nes Danj";
void logSDLError(std::ostream& os, const std::string &msg, bool fatal)
{
os << msg << " Error: " << SDL_GetError() << std::endl;
if (fatal) {
SDL_Quit();
exit(1);
}
}
void initSDL(SDL_Window* &window, SDL_Renderer* &renderer)
{
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
logSDLError(std::cout, "SDL_Init", true);
window = SDL_CreateWindow(WINDOW_TITLE.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
//window = SDL_CreateWindow(WINDOW_TITLE.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_FULLSCREEN_DESKTOP);
if (window == nullptr)
logSDLError(std::cout, "CreateWindow", true);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
//SDL_Renderer *renderer = SDL_CreateSoftwareRenderer(SDL_GetWindowSurface(window));
if (renderer == nullptr)
logSDLError(std::cout, "CreateRenderer", true);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
}
void quitSDL(SDL_Window* window, SDL_Renderer* renderer)
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
void waitUntilKeyPressed()
{
SDL_Event e;
while (true) {
if ( SDL_WaitEvent(&e) != 0 &&
(e.type == SDL_KEYDOWN || e.type == SDL_QUIT) )
return;
SDL_Delay(100);
}
}
SDL_Texture* loadTexture(std::string path, SDL_Renderer* renderer)
{
SDL_Texture* newTexture = nullptr;
//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load(path.c_str() );
if ( loadedSurface == nullptr)
std::cout << "Unable to load image " << path << " SDL_image Error: "
<< IMG_GetError() << std::endl;
else
{
//Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface( renderer, loadedSurface);
if (newTexture == nullptr)
{
std::cout << "Unable to create texture from " << path
<< "SDL Error: " << SDL_GetError() << std::endl;
}
// Get rid of old loaded surface
SDL_FreeSurface( loadedSurface);
}
return newTexture;
}