Repository navigation
Expand file tree
/
Copy pathSDL_BaseObj.cpp
More file actions
66 lines (55 loc) · 1.51 KB
/
Copy pathSDL_BaseObj.cpp
File metadata and controls
66 lines (55 loc) · 1.51 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
//The software was written by me, with references ideas from :
//Reference source 1 : LazyFoo's Tutorial (initSDL, Game-Loop, Texture load, Player with SpriteSheet load, Musics, Text)
//Reference source 2 : Phattrienphanmem123az's Tutorial (Map algorithm, Collision Detection)
//Reference source 3 : Tran Thi Minh Chau's Tutorial (Way to organize, sort .h and .cpp files)
//05 - 2020
#include "stdafx.h"
#include "SDL_BaseObj.h"
#include "SDL_DisplayText.h"
#include <iostream>
BaseObject::BaseObject()
{
pObject = NULL;
rect_.x = 0;
rect_.y = 0;
rect_.w = 0;
rect_.h = 0;
}
BaseObject::~BaseObject()
{
Free();
}
bool BaseObject::LoadIMG(std::string path, SDL_Renderer* screen)
{
Free(); //Remember this before debugging. Buffer overflow error. DS:04.28.20
SDL_Texture* newTexture = NULL;
SDL_Surface* loadSurface = IMG_Load(path.c_str());
if (loadSurface != NULL)
{
SDL_SetColorKey(loadSurface, SDL_TRUE, SDL_MapRGB(loadSurface->format, 255, 255, 255));
newTexture = SDL_CreateTextureFromSurface( screen, loadSurface);
if (newTexture != NULL)
{
rect_.w = loadSurface->w;
rect_.h = loadSurface->h;
}
SDL_FreeSurface(loadSurface);
}
pObject = newTexture;
return pObject != NULL;
}
void BaseObject::Render(SDL_Renderer* des, const SDL_Rect* clip)
{
SDL_Rect renderScreen = { rect_.x, rect_.y, rect_.w, rect_.h };
SDL_RenderCopy(des, pObject, clip, &renderScreen);
}
void BaseObject::Free()
{
if (pObject != NULL)
{
SDL_DestroyTexture(pObject);
pObject = NULL;
rect_.w = 0;
rect_.h = 0;
}
}