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
|
#include "gfx.h"
//all the global variables first set here
SDL_Window *mwindowp; // main window were everything foes
SDL_DisplayMode * screenp;
SDL_Renderer * renderp;
TTF_Font * fontp;
SDL_Surface * msurface;
bool surface;
int maxx;
int maxy;
int initgfx(int x, int y,const char * title) {
//initializing SDL 2
SDL_Init(SDL_INIT_EVERYTHING);
//sets global variables for ease of access later
//they are 100 by 200 for now because DisplayMode takes long to implement
//screen resizes are handled by sdl events
//we're not gonna hcange these for now
maxx = x;
maxy = y;
//makes sdl compatible with opengl(for later use)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
//creates window
mwindowp = SDL_CreateWindow(title, 0, 0, maxx, maxy, SDL_WINDOW_OPENGL);
//renderer to write to
//the int selects which drivers, -1 just selects first applicatble
renderp = SDL_CreateRenderer(mwindowp, -1,SDL_RENDERER_ACCELERATED);
//load truetype font
int err = TTF_Init();
fontp = TTF_OpenFont("Terminus.ttf",18);
if (fontp == NULL) {
printf("font failed to load\n");
printf("ttf error %s",TTF_GetError());
}
return 0;
}
int surfacerender(int w, int h) {
msurface = SDL_CreateRGBSurface(0, w, h, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
}
//two draw functions for convenience
int drawimg(int x, int y, int w, int h,SDL_Texture * tex) {
SDL_Rect r;
r.x = x;
r.y = y;
r.w = w;
r.h = h;
//null means entire source texture
SDL_RenderCopy(renderp, tex, NULL, &r);
return 0;
}
int drawimg(SDL_Rect r, SDL_Texture * tex) {
SDL_RenderCopy(renderp, tex, NULL, &r);
return 0;
}
SDL_Texture * loadtexture(const char * file) {
SDL_Surface * tsp;
SDL_Texture * ttp;
tsp = SDL_LoadBMP(file);
ttp = SDL_CreateTextureFromSurface(renderp, tsp);
SDL_SetTextureBlendMode(ttp, SDL_BLENDMODE_BLEND);
SDL_FreeSurface(tsp);
return ttp;
}
int drawimg(int x, int y, int w, int h, const char * file) {
SDL_Texture * ttp = loadtexture(file);
drawimg(x,y,w,h,ttp);
SDL_DestroyTexture(ttp);
}
int drawrect (int x, int y, int w, int h, const char * color) {
std::string str;
str = color;
SDL_Rect r;
r.x = x;
r.y = y;
r.w = w;
r.h = h;
if (!str.compare("red")) {
SDL_SetRenderDrawColor(renderp,255,0,0,255);
}
if (!str.compare("blue")) {
SDL_SetRenderDrawColor(renderp,0,0,255,255);
}
if (!str.compare("orange")) {
SDL_SetRenderDrawColor(renderp,255,180,20,255);
}
if (!str.compare("white")) {
SDL_SetRenderDrawColor(renderp,255,255,255,255);
}
if (!str.compare("black")) {
SDL_SetRenderDrawColor(renderp,0,0,0,255);
}
SDL_RenderFillRect(renderp,&r);
SDL_SetRenderDrawColor(renderp,0,0,0,255);
return 0;
}
int drawrect (int x, int y, int w, int h,int r, int g, int b, int a) {
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
SDL_SetRenderDrawColor(renderp,r,g,b,a);
SDL_SetRenderDrawBlendMode(renderp, SDL_BLENDMODE_BLEND);
SDL_RenderFillRect(renderp,&rect);
SDL_SetRenderDrawColor(renderp,0,0,0,255);
return 0;
}
//draws text
int drawtext(int x, int y,const char * text, SDL_Color c) {
//surface the font renderer writes to
SDL_Surface * sp;
//color of text
SDL_Color co;
co.r = 100;
co.g = 100;
co.b = 100;
co.a = 100;
//renders text with global font
sp = TTF_RenderText_Solid(fontp, text, c);
//texture to actually draw
SDL_Texture * tp;
tp = SDL_CreateTextureFromSurface(renderp, sp);
//the surface has to be freed because it is only used once and the pointer
//to it was declared in a funcion
SDL_FreeSurface(sp);
//finding the width and height of the text
int w;
int h;
SDL_QueryTexture(tp, NULL, NULL, &w, &h);
//drawing the texture
drawimg(x, y, w, h, tp);
SDL_DestroyTexture(tp);
}
//this is where almost all of the grame code is
int updategfx() {
//checks the states of the buttons and handles them accordingly
//clears renderer buffer
SDL_RenderPresent(renderp);
return 0;
}
int cleargfx() {
SDL_RenderClear(renderp);
}
//quits all the libraries and releases some memory
int quitgfx() {
//release of context
SDL_DestroyRenderer(renderp);
TTF_Quit();
SDL_Quit();
return 0;
}
|