-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
197 lines (160 loc) · 4.3 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h> //For PNG support
#include <stdio.h> //For printf
#include <string>
//Global constants
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
//Global variables
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
//Forward declarations
bool init();
//Classes
class LTexture // LTexture == Lazy Texture, change this convention with a better one
{
public:
LTexture(); //Constructor - Used to initialize mVariables - Look these up to see what else we can do with them
~LTexture(); //Destructor - Used to deallocate pointers - Look these up to see what else we can do with them
void free(); // Deallocates texture
bool LoadTexture(std::string path); //Load a texture from a file, return false if fail
void Render(int x, int y); //Renders texture at X and Y
int getWidth();
int getHeight();
private:
SDL_Texture* mTexture;
int mWidth;
int mHeight;
};
LTexture::LTexture()
{
mTexture = nullptr;
mWidth = 0;
mWidth = 0;
}
LTexture::~LTexture()
{
free();
}
void LTexture::free()
{
SDL_DestroyTexture( mTexture );
mTexture = nullptr;
}
bool LTexture::LoadTexture(std::string path)
{
bool success = true;
SDL_Surface* image = NULL;
image = IMG_Load( path.c_str() );
if(!image)
{
printf( "Could not load image %s! IMG_Error:%s\n", path.c_str(), IMG_GetError() );
success = false;
}
else
{
mWidth = image->w;
mHeight = image->h;
mTexture = SDL_CreateTextureFromSurface( gRenderer, image );
}
SDL_FreeSurface( image );
return success;
};
void LTexture::Render( int x, int y )
{
SDL_Rect dest
{
.x = x,
.y = y,
.w = mWidth,
.h = mHeight
};
SDL_RenderCopy( gRenderer, mTexture, NULL, &dest);
}
int LTexture::getHeight()
{
return mHeight;
}
int LTexture::getWidth()
{
return mWidth;
}
int main( int argc, char* args[] )
{
//Initialize SDL
if( !init() )
{
printf( "SDL could not initialize!\n" );
}
bool isRunning = true;
SDL_Event e;
LTexture logo;
logo.LoadTexture( "images/cb.bmp" );
while( isRunning ) //Main loop
{
while( SDL_PollEvent( &e ) )
{
if( e.type == SDL_QUIT )
{
isRunning = false;
}
}
SDL_RenderClear( gRenderer );
//logo.Render( (WINDOW_WIDTH - logo.getWidth() ) / 2 , ( WINDOW_HEIGHT - logo.getHeight() ) / 2 ) ;
int x = 0;
int y = 0;
SDL_GetMouseState(&x, &y);
x = x - ( logo.getWidth() / 2 ) ;
y = y - ( logo.getHeight() / 2 );
logo.Render( x, y );
SDL_RenderPresent( gRenderer );
}
//Destroy Renderer
SDL_DestroyRenderer( gRenderer );
//Destroy Window
SDL_DestroyWindow( gWindow );
//Close SDL and all subsystems
SDL_Quit();
return 0;
}
bool init()
{
bool success = true;
//Initialize SDL with video subsystem
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to initialize SDL! SDL Error:%s\n", SDL_GetError() );
success = false;
}
else
{
//Create a window
gWindow = SDL_CreateWindow( "SDL-Test", NULL, NULL, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL)
{
printf( "Failed to create window! SDL Error:%s\n", SDL_GetError() );
success = false;
}
else
{
gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
if( gRenderer == NULL)
{
printf( "Failed to create renderer! SDL Error:%s\n", SDL_GetError() );
success = false;
}
//Initialize SDL_image
int flags = IMG_INIT_PNG;
if( !( IMG_Init(flags) ) & flags)
{
printf( "Unable to initialize SDL_image! IMG Error:%s\n", IMG_GetError() );
success = false;
}
//Clear the display
SDL_SetRenderDrawColor( gRenderer, 0xff, 0xff, 0xff, 0xff);
SDL_RenderClear( gRenderer );
SDL_RenderPresent( gRenderer );
}
}
return success;
}