-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·199 lines (153 loc) · 3.98 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·199 lines (153 loc) · 3.98 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
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
198
199
#include <iostream>
#include "SDL2/SDL.h"
#include "SDL2/SDL_image.h"
#include "Character.h"
#include "LTexture.h"
#include "LTimer.h"
#include "Map.hpp"
using namespace std;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGT = 480;
const float cWalkSpeed = 400.0f;
const float cJumpSpeed = -800.0f;
const float cHalfSizeY = 20.0f;
const float cHalfSizeX = 6.0f;
Character c;
SDL_Window* gWindow;
SDL_Renderer* gRenderer;
LTexture gBackGroundTexture;
LTimer stepTimer;
float timeStep = 0;
void CharacterInit()
{
c.mJumpSpeed = cJumpSpeed;
c.mWalkSpeed = cWalkSpeed;
}
bool init()
{
bool success = true;
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
success = false;
cout << "Error initialing SDL libraries " << SDL_GetError() << endl;
}
else
{
gWindow = SDL_CreateWindow("Platformer game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGT, SDL_WINDOW_SHOWN);
if(gWindow == NULL)
{
success = false;
cout << "Error creatin the window " << SDL_GetError() << endl;
}
else
{
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if(gRenderer == NULL)
{
success = false;
cout << "Error initialing renderer " << SDL_GetError() << endl;
}
else
{
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
int imageFlags = IMG_INIT_PNG;
if(!(IMG_Init(imageFlags) &imageFlags))
{
cout << "Error initialing image library " << IMG_GetError() << endl;
}
}
}
}
return success;
}
bool loadMedia()
{
bool success = true;
if(!c.mTexture.loadFromFile("images/mario.png"))
{
success = false;
cout << "Error loading foo texture " << IMG_GetError() << endl;
}
else
{
c.gCharacterSpirteClips[ c.Stand ].x = 0;
c.gCharacterSpirteClips[ c.Stand ].y = 0;
c.gCharacterSpirteClips[ c.Stand ].w = 40;
c.gCharacterSpirteClips[ c.Stand ].h = 56;
//Set top right sprite
c.gCharacterSpirteClips[ c.Walk ].x = 42;
c.gCharacterSpirteClips[ c.Walk ].y = 0;
c.gCharacterSpirteClips[ c.Walk ].w = 44;
c.gCharacterSpirteClips[ c.Walk ].h = 60;
//Set bottom left sprite
c.gCharacterSpirteClips[ c.Walk2Frame ].x = 88;
c.gCharacterSpirteClips[ c.Walk2Frame ].y = 0;
c.gCharacterSpirteClips[ c.Walk2Frame ].w = 30;
c.gCharacterSpirteClips[ c.Walk2Frame ].h = 60;
//Set bottom right sprite
c.gCharacterSpirteClips[ c.Jump ].x = 121;
c.gCharacterSpirteClips[ c.Jump ].y = 0;
c.gCharacterSpirteClips[ c.Jump ].w = 47;
c.gCharacterSpirteClips[ c.Jump ].h = 60;
}
if(!gBackGroundTexture.loadFromFile("images/background.png"))
{
success = false;
cout << IMG_GetError() << endl;
}
else
{
gBackGroundTexture.mWidth = SCREEN_WIDTH;
gBackGroundTexture.mHeight = SCREEN_HEIGT;
}
return success;
}
void close()
{
SDL_DestroyWindow(gWindow);
SDL_DestroyRenderer(gRenderer);
SDL_Quit();
IMG_Quit();
}
int main( int argc, char* args[] )
{
SDL_Event e;
bool quit = false;
if(init())
{
if(loadMedia())
{
CharacterInit();
while(!quit)
{
while(SDL_PollEvent(&e) != 0)
{
if(e.type == SDL_QUIT)
{
quit = true;
}
else if(e.type == SDL_KEYDOWN)
{
c.mInputs[SDL_GetScancodeFromKey(e.key.keysym.sym)] = true;
}
else if(e.type == SDL_KEYUP)
{
c.mInputs[SDL_GetScancodeFromKey(e.key.keysym.sym)] = false;
}
}
timeStep = stepTimer.getTicks() / 1000.f;
SDL_RenderClear(gRenderer);
gBackGroundTexture.render(START_POSITION.x - c.mPosition.x, START_POSITION.y - c.mPosition.y);
c.CharacterUpdate();
stepTimer.start();
SDL_RenderPresent(gRenderer);
if(timeStep < 1/60.f)
{
SDL_Delay(1000 * (1/60.f - timeStep));
}
}
}
}
close();
return 0;
}