-
Notifications
You must be signed in to change notification settings - Fork 1
/
Graphics.cpp
107 lines (98 loc) · 2.23 KB
/
Graphics.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
/*
* Graphics.cpp
*
* Created on: Aug 25, 2016
* Author: eric
*/
#include"Graphics.h"
/* init()
* Sets sdl video mode
* returns false if failed
*/
bool Graphics::init(int screenWidth, int screenHeight, bool isFullscreen){
if(isFullscreen){
backbuffer = SDL_SetVideoMode(screenWidth, screenHeight, 32, SDL_SWSURFACE|SDL_FULLSCREEN);
} else {
backbuffer = SDL_SetVideoMode(screenWidth, screenHeight, 32, SDL_SWSURFACE);
}
if(backbuffer == NULL){
printf("Failed to initialize graphics!\n");
return false;
}
return true;
}
/* drawPixel()
* draws a pixel primitive
* returns if backbuffer is null, if the backbuffer is locked or the
* coordinates are out of range
*/
void Graphics::drawPixel(int x,int y,int r,int g,int b){
if(backbuffer == NULL){
return;
}
if(SDL_MUSTLOCK(backbuffer)){
if(SDL_LockSurface(backbuffer)<0){
return;
}
}
if(x >= backbuffer->w||x<0||y>=backbuffer->h||y<0){
return;
}
Uint32 *buffer;
Uint32 color;
color = SDL_MapRGB(backbuffer->format, r, g, b);
buffer = (Uint32*)backbuffer->pixels + y*backbuffer->pitch/4+x;
*buffer = color;
if(SDL_MUSTLOCK(backbuffer)){
SDL_UnlockSurface(backbuffer);
}
}
/* drawRect()
* uses fillRect() to fill 4 "rects" in the shape of rectangle with a line thickness
* of 1 pixel
*/
void Graphics::drawRect(int x, int y, int width, int height, int r, int g, int b){
fillRect(x, y, width, 1, r, g , b);
fillRect(x, y+height-1, width, 1, r, g, b);
fillRect(x, y, 1, height, r, g, b);
fillRect(x+width-1, y, 1, height, r, g, b);
}
/* fillRect()
*
*/
void Graphics::fillRect(int x, int y, int width, int height, int r, int g, int b){
if (backbuffer == NULL){
return;
}
Uint32 color;
color = SDL_MapRGB(backbuffer->format, r, g, b );
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = width;
rect.h = height;
SDL_FillRect(backbuffer, &rect, color);
}
/* clear()
*
*/
void Graphics::clear(int r, int g, int b){
if(backbuffer == NULL){
return;
}
Uint32 color;
color = SDL_MapRGB(backbuffer->format, r, g, b);
SDL_FillRect(backbuffer, NULL, color);
}
int Graphics::getWidth(){
return width;
}
int Graphics::getHeight(){
return height;
}
SDL_Surface* Graphics::getBackbuffer(){
return backbuffer;
}
void Graphics::flip(){
SDL_Flip(backbuffer);
}