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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <string>
namespace SDLWrapper
{
class Window
{
SDL_Window *handle;
public:
Window();
Window(const std::string& name, int x, int y); // you can add the other flags here instead of hard coding them
~Window();
Window(const Window&) = delete;
Window& operator=(const Window&) = delete;
Window(Window&&);
Window& operator=(Window&&);
operator SDL_Window*();
};
class Renderer
{
SDL_Renderer* handle;
public:
Renderer(Window& window);
~Renderer();
Renderer(const Renderer&) = delete;
Renderer& operator=(const Renderer&) = delete;
Renderer(Renderer&&);
Renderer& operator=(Renderer&&);
operator SDL_Renderer*();
};
class Surface
{
SDL_Surface* handle;
public:
Surface(const std::string& filename);
~Surface();
Surface(const Surface&) = delete;
Surface& operator=(const Surface&) = delete;
Surface(Surface&&);
Surface& operator=(Surface&&);
operator SDL_Surface*();
};
class Texture
{
SDL_Texture* handle;
public:
Texture(Renderer& renderer, Surface& surface);
~Texture();
Texture(const Texture&) = delete;
Texture& operator=(const Texture&) = delete;
Texture(Texture&&);
Texture& operator=(Texture&&);
operator SDL_Texture*();
};
//-----------------------------------------------------------------------
Window::Window() :
handle(nullptr)
{
}
Window::Window(const std::string& name, int x, int y) :
handle(SDL_CreateWindow(name.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, x, y, 0))
{
}
Window::~Window()
{
if (handle)
SDL_DestroyWindow(handle);
}
Window::Window(Window&& n) :
handle(n.handle)
{
handle = nullptr;
}
Window& Window::operator=(Window&& n)
{
if (this != &n)
{
if (handle)
SDL_DestroyWindow(handle);
handle = n.handle;
n.handle = nullptr;
}
return *this;
}
Window::operator SDL_Window*()
{
return handle;
}
//-----------------------------------------------------------------------
Renderer::Renderer(Window& window) :
handle(SDL_CreateRenderer(window, -1, 0))
{
}
Renderer::~Renderer()
{
if (handle)
SDL_DestroyRenderer(handle);
}
Renderer::Renderer(Renderer&& n) :
handle(n.handle)
{
n.handle = nullptr;
}
Renderer& Renderer::operator=(Renderer&& n)
{
if (this != &n)
{
if (handle)
SDL_DestroyRenderer(handle);
handle = n.handle;
n.handle = nullptr;
}
return *this;
}
Renderer::operator SDL_Renderer*()
{
return handle;
}
//-----------------------------------------------------------------------
Surface::Surface(const std::string& filename) :
handle(IMG_Load(filename.c_str()))
{
}
Surface::~Surface()
{
if (handle)
SDL_FreeSurface(handle);
}
Surface::Surface(Surface&& n) :
handle(n.handle)
{
n.handle = nullptr;
}
Surface& Surface::operator=(Surface&& n)
{
if (this != &n)
{
if (handle)
SDL_FreeSurface(handle);
handle = n.handle;
n.handle = nullptr;
}
return *this;
}
Surface::operator SDL_Surface*()
{
return handle;
}
//-----------------------------------------------------------------------
Texture::Texture(Renderer& renderer, Surface& surface) :
handle(SDL_CreateTextureFromSurface(renderer, surface))
{
}
Texture::~Texture()
{
if (handle)
SDL_DestroyTexture(handle);
}
Texture::Texture(Texture&& n) :
handle(n.handle)
{
n.handle = nullptr;
}
Texture& Texture::operator=(Texture&& n)
{
if (this != &n)
{
if (handle)
SDL_DestroyTexture(handle);
handle = n.handle;
n.handle = nullptr;
}
return *this;
}
Texture::operator SDL_Texture*()
{
return handle;
}
}
int main(int argc, char *argv[])
{
using namespace SDLWrapper;
// represents if the user has or has not requested to quit the program
bool quit = false;
// the main event for the program
SDL_Event event;
// unsure of these
SDL_Init(SDL_INIT_VIDEO);
IMG_Init(IMG_INIT_PNG);
// creates a window and renderer. the renderer is everything displayed in the window
Window window("Harvester Odyssey", 1200, 800);
Renderer renderer(window);
// sprite for running left
Surface runLeftImage("runLeftAnimation.png");
Texture runLeftTexture(renderer, runLeftImage);
// sprite for running right
Surface runRightImage("runRightAnimation.png");
Texture runRightTexture(renderer, runRightImage);
// sprite for standing left
Surface idleLeftImage("idleLeftAnimation.png");
Texture idleLeftTexture(renderer, idleLeftImage);
// sprite for standing right
Surface idleRightImage("idleRightAnimation.png");
Texture idleRightTexture(renderer, idleRightImage);
// sprite for jumping right
Surface jumpRightImage("jumpRightAnimation.png");
Texture jumpRightTexture(renderer, jumpRightImage);
// sprite for jumping left
Surface jumpLeftImage("jumpLeftAnimation.png");
Texture jumpLeftTexture(renderer, jumpLeftImage);
// represents the main character sprite
SDL_Texture * animation = idleRightTexture;
// represents if the sprite was last facing left or not
bool facingLeft = false;
// sets the background color of the renderer to gray
SDL_SetRenderDrawColor(renderer, 75, 75, 75, 75);
//clears the renderer of everything except the background color
SDL_RenderClear(renderer);
// the x and y position of there the sprite will be drawn
int xCoord = 10;
int yCoord = 700;
// gets the time passed since the program started and stores it in this variable
unsigned int time = SDL_GetTicks();
// the main game loop which runs as long as the user doesn't request to quit the program
while (!quit)
{
// recieves the next event in the que
SDL_PollEvent(&event);
// swich that closes the program if the user requests to do so
switch (event.type)
{
case SDL_QUIT:
quit = true;
break;
default:
break;
}
// ANIMATION AND MOVEMENT MANAGMENT //
// makes the sprite face the appropriate direction when no buttons are being pressed
if (facingLeft == true)
{
animation = idleLeftTexture;
}
else
{
animation = idleRightTexture;
}
// this sets the number of frames and frame rate of the sprite
Uint32 ticks = SDL_GetTicks();
Uint32 sprite = (ticks / 100) % 1;
// used to check the state of the keyboard
const Uint8 *keyState = SDL_GetKeyboardState(NULL);
// adjusts animation and movement based on key input
if (keyState[SDL_SCANCODE_RIGHT])
{
facingLeft = false;
animation = runRightTexture;
sprite = (ticks / 100) % 6;
if (SDL_GetTicks() > time + 4)
{
++xCoord;
time = SDL_GetTicks();
}
}
else if (keyState[SDL_SCANCODE_LEFT])
{
facingLeft = true;
animation = runLeftTexture;
sprite = (ticks / 100) % 6;
if (SDL_GetTicks() > time + 4)
{
--xCoord;
time = SDL_GetTicks();
}
}
if (keyState[SDL_SCANCODE_UP])
{
if (facingLeft == true)
{
animation = jumpLeftTexture;
}
else
{
animation = jumpRightTexture;
}
sprite = (ticks / 100) % 4;
}
// controls which clip of the sprite sheet is used
SDL_Rect srcrect = { sprite * 50, 0, 50, 75 };
// controls the destination of the sprite sheet to be rendered
SDL_Rect dstrect = { xCoord, yCoord, 50, 75 };
// clears the renderer to the background color
SDL_RenderClear(renderer);
// prints the proper animation to the proper location based on the above information
SDL_RenderCopy(renderer, animation, &srcrect, &dstrect);
// shows renderer within the window
SDL_RenderPresent(renderer);
}
IMG_Quit();
SDL_Quit();
// terminates the program
return 0;
}
|