-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphics.cpp
More file actions
121 lines (96 loc) · 2.88 KB
/
Graphics.cpp
File metadata and controls
121 lines (96 loc) · 2.88 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
#include "Graphics.h"
#include "Constants.h"
#include <string>
#include <iostream>
Graphics::Graphics(){
setCamera(0,0,0,0);
}
Uint32 Graphics::getPixel( SDL_Surface *surface, int x, int y ){
//Convert the pixels to 32 bit
Uint32 *pixels = (Uint32 *)surface->pixels;
//Get the requested pixel
return pixels[ ( y * surface->w ) + x ];
}
void Graphics::renderText(std::string text, int r, int g, int b, int x, int y, int w,int h){
TTF_Font *font = TTF_OpenFont("/Library/Fonts/Georgia.ttf",12);
if(font == NULL)
return;
SDL_Color text_color = {r,g,b};
const char* text_c = text.c_str();
SDL_Surface *s = TTF_RenderText_Solid(font,text_c,text_color);
SDL_Texture* t = SDL_CreateTextureFromSurface(renderer,s);
SDL_Rect dst = makeRect(x,y,w,h);
SDL_RenderCopy(renderer,t,NULL,&dst);
//Free stuff up
TTF_CloseFont(font);
SDL_FreeSurface(s);
SDL_DestroyTexture(t);
}
SDL_Surface* Graphics::getSurfaceFromPath(std::string path){
SDL_Surface* loadSurf = IMG_Load(path.c_str());
return loadSurf;
}
SDL_Texture* Graphics::getTextureFromPath(std::string path){
SDL_Texture* tex = NULL;
SDL_Surface* loadSurf = IMG_Load(path.c_str());
tex = SDL_CreateTextureFromSurface(renderer,loadSurf);
SDL_FreeSurface(loadSurf);
return tex;
}
void Graphics::setup(const std::string title,int width, int height,int xLoc,int yLoc){
SDL_Init(SDL_INIT_EVERYTHING);
TTF_Init();
window = SDL_CreateWindow(title.c_str(),xLoc,yLoc,width,height,0);
renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
}
void Graphics::close(){
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
TTF_Quit();
}
void Graphics::render(SDL_Texture * t, SDL_Rect * src, SDL_Rect * dst){
SDL_RenderCopy(renderer,t,src,dst);
}
void Graphics::renderWithFlip(SDL_Texture * t, SDL_Rect * src, SDL_Rect * dst,SDL_RendererFlip flip){
SDL_RenderCopyEx(renderer,t,src,dst,0,0,flip);
}
SDL_Rect Graphics::makeRect(int x, int y, int w, int h){
SDL_Rect ret;
ret.x = x;
ret.y = y;
ret.w = w;
ret.h = h;
return ret;
}
void Graphics::displayRect(SDL_Rect r){
std::cout << "Rect:\n";
std::cout << "X: " << r.x << std::endl;
std::cout << "Y: " << r.y << std::endl;
std::cout << "W: " << r.w << std::endl;
std::cout << "H: " << r.h << std::endl;
}
void Graphics::setCamera(int x, int y, int w, int h){
camera.x = x;
camera.y = y;
camera.w = w;
camera.h = h;
}
void Graphics::moveCamera(int x, int y){
camera.x += x;
camera.y += y;
if(camera.x <= 0){
camera.x = 0;
}
if(camera.y <= 0){
camera.y= 0;
}
}
void Graphics::renderFromLocation(SDL_Texture* t, SDL_Rect * src, int x, int y, int w, int h){
SDL_Rect dst = makeRect(x-camera.x,y-camera.y,w,h);
render(t,src,&dst);
}
void Graphics::renderFromLocationWithFlip(SDL_Texture* t, SDL_Rect * src, int x, int y, int w, int h){
SDL_Rect dst = makeRect(x-camera.x,y-camera.y,w,h);
renderWithFlip(t,src,&dst,SDL_FLIP_HORIZONTAL);
}