Skip to content

Commit 3a6ab9a

Browse files
committed
initial commit, version 0.03a
1 parent 81a3d43 commit 3a6ab9a

File tree

13 files changed

+1578
-0
lines changed

13 files changed

+1578
-0
lines changed

Diff for: LICENSE

100644100755
File mode changed.

Diff for: Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
all:
2+
gcc main.cpp -Wall -ltag_c -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer `sdl2-config --cflags --libs` -Os -o madamp
3+
debug:
4+
gcc main.cpp -Wall -ltag_c -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer `sdl2-config --cflags --libs` -g3 -ggdb -o madamp
5+
6+
clean:
7+
rm madamp

Diff for: README.md

100644100755
File mode changed.

Diff for: engine.h

+216
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
class engine{
2+
private:
3+
// Screen width and height
4+
unsigned short int SCREEN_WIDTH, SCREEN_HEIGHT;
5+
SDL_Window *window;
6+
7+
bool debug;
8+
9+
bool gfx_init;
10+
bool sfx_init;
11+
12+
// Used for calculation of FPS in current second, do not use this!
13+
uint16_t fps;
14+
15+
// Used to store result for FPS in last second. Use this one!
16+
uint16_t fps_count;
17+
18+
// Number of FPS we're limiting to, if at all
19+
uint8_t fps_limit;
20+
21+
// How many milliseconds between frames
22+
uint8_t frame_interval;
23+
24+
// When did we last draw a frame?
25+
uint32_t last_drawn_frame_timestamp;
26+
27+
void calculate_fps(void);
28+
public:
29+
engine(int width, int height, uint8_t max_fps);
30+
bool init_gfx(void);
31+
bool init_sfx(void);
32+
bool clear_screen(void);
33+
void destroy(void);
34+
void flush_screen(void);
35+
void disable_debug_mode(void);
36+
SDL_Renderer *renderer;
37+
void loop(void);
38+
void draw_square(int x1, int y1, int x2, int y2, SDL_Color *color);
39+
void debug_message(const char* msg);
40+
unsigned short int get_height(void);
41+
unsigned short int get_width(void);
42+
43+
};
44+
45+
46+
47+
48+
/**
49+
* Initializer for engine object
50+
* @param {integer} width - screen width to initialize with
51+
* @param {integer} height - screen height to initialize with
52+
*/
53+
engine::engine( int width, int height, uint8_t max_fps ){
54+
SCREEN_WIDTH = width;
55+
SCREEN_HEIGHT = height;
56+
window = NULL;
57+
renderer = NULL;
58+
gfx_init = false;
59+
fps=0;
60+
fps_count=0;
61+
last_drawn_frame_timestamp=0;
62+
fps_limit = max_fps;
63+
frame_interval = (max_fps == 0 ? 0 : (int)(1000 / fps_limit));
64+
debug = true;
65+
}
66+
67+
unsigned short int engine::get_height(void){
68+
return SCREEN_HEIGHT;
69+
}
70+
71+
unsigned short int engine::get_width(void){
72+
return SCREEN_WIDTH;
73+
}
74+
75+
76+
void engine::debug_message(const char* msg){
77+
if (debug){
78+
printf(msg);
79+
fflush(stdout);
80+
}
81+
82+
83+
}
84+
85+
bool engine::init_gfx(void){
86+
printf("Init Video\n");
87+
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
88+
printf( "SDL init failed.\n" );
89+
exit(2);
90+
return false;
91+
}
92+
93+
printf("Creating Window\n");
94+
window = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_HEIGHT,SCREEN_WIDTH, SDL_WINDOW_FULLSCREEN);
95+
if (window == NULL){
96+
printf("Window generating failed\n");
97+
return false;
98+
}
99+
100+
printf("Creating renderer\n");
101+
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
102+
if (renderer == NULL){
103+
printf("Failed to create renderer\n");
104+
return false;
105+
}
106+
107+
printf("GFX init complete\n");
108+
gfx_init = true;
109+
110+
SDL_SetRenderDrawColor(renderer,0,0,0,SDL_ALPHA_OPAQUE);
111+
112+
return true;
113+
114+
}
115+
116+
void engine::flush_screen(void){
117+
if (gfx_init){
118+
SDL_RenderPresent(renderer);
119+
}
120+
121+
}
122+
123+
bool engine::clear_screen(void){
124+
if (gfx_init){
125+
SDL_RenderClear(renderer);
126+
return true;
127+
}
128+
return false;
129+
130+
}
131+
132+
133+
void engine::destroy(void){
134+
if (gfx_init){
135+
SDL_DestroyRenderer(renderer);
136+
SDL_DestroyWindow(window);
137+
}
138+
if (sfx_init){
139+
Mix_Quit();
140+
}
141+
SDL_Quit();
142+
143+
}
144+
145+
void engine::disable_debug_mode(void){
146+
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_CRITICAL);
147+
}
148+
149+
150+
/**
151+
* function basically calculates current framerate
152+
* detects when a second has passed and resets the counter
153+
* applies whatever value we had.
154+
*/
155+
void engine::calculate_fps(void){
156+
if( (int)SDL_GetTicks()/1000 > (int)last_drawn_frame_timestamp/1000 ){
157+
fps_count = fps;
158+
fps = 0;
159+
}
160+
fps++;
161+
}
162+
163+
164+
void engine::loop(void){
165+
// Calculate what tick are we drawing the next frame at
166+
uint32_t next_frame_timestamp = last_drawn_frame_timestamp+frame_interval;
167+
168+
// Make sure the time we're supposed to draw at is upon us or passed:
169+
if (SDL_GetTicks() >= next_frame_timestamp){
170+
171+
// Calculate FPS
172+
calculate_fps();
173+
174+
// Make sure to get the new window surface and update it
175+
flush_screen();
176+
177+
// Update last drawn timestamp
178+
last_drawn_frame_timestamp = SDL_GetTicks();
179+
180+
} else {
181+
// So for this function I'm calculating WHEN we should draw the next frame
182+
// And using SDL_Delay to wait from now until then, to then draw the next frame.
183+
SDL_Delay( (next_frame_timestamp) - SDL_GetTicks() );
184+
185+
}
186+
}
187+
188+
189+
void engine::draw_square(int x1, int y1, int x2, int y2, SDL_Color *color){
190+
SDL_Rect square;
191+
square.x = x1;
192+
square.y = y1;
193+
square.w = x2-x1;
194+
square.h = y2-y1;
195+
SDL_SetRenderDrawColor(renderer, color->r,color->g,color->b, color->a);
196+
SDL_RenderFillRect(renderer, &square);
197+
}
198+
199+
200+
201+
bool engine::init_sfx(void){
202+
if (SDL_Init(SDL_INIT_AUDIO) < 0){
203+
printf("Unable to initialize audio\n");
204+
return false;
205+
}
206+
207+
if (Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,2,128) == -1){
208+
printf("Unable to initialize SDL_Mixer\n");
209+
return false;
210+
}
211+
212+
printf("Init Audio\n");
213+
sfx_init = true;
214+
return true;
215+
}
216+

Diff for: gfx/background.h

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
class background{
2+
friend class Sprite;
3+
4+
private:
5+
bool loaded;
6+
SDL_Rect size;
7+
SDL_Rect temp;
8+
SDL_Texture *Texture;
9+
SDL_Renderer *randr;
10+
engine *ref;
11+
public:
12+
background(const char* file, engine EngineRef);
13+
void draw(void);
14+
void destroy(void);
15+
};
16+
17+
18+
void background::draw(void){
19+
20+
21+
if(loaded){
22+
SDL_RenderCopy(randr, Texture, NULL, &temp);
23+
} else {
24+
printf("Attempted to draw a background that is not yet loaded");
25+
}
26+
}
27+
28+
29+
30+
background::background(const char* file, engine EngineRef){
31+
32+
// Reset flag
33+
loaded = false;
34+
35+
36+
// Copy renderer reference
37+
randr = EngineRef.renderer;
38+
39+
// Load picture
40+
SDL_Surface *image_surface = IMG_Load(file);
41+
42+
// Create a new surface for temporary application
43+
SDL_Surface *temp_surface = SDL_CreateRGBSurface(0,EngineRef.get_width(),EngineRef.get_height(),32,0,0,0,0); // for the looping and then texturing
44+
45+
// Load picture
46+
if (image_surface != NULL){
47+
loaded = true;
48+
} else {
49+
printf("Failed at loading %s\n", file);
50+
exit(1);
51+
}
52+
53+
// Original bg size
54+
size.x=0;
55+
size.y=0;
56+
size.h=image_surface->h;
57+
size.w=image_surface->w;
58+
59+
// SDL_Rect used for moving the surface around and apply it
60+
SDL_Rect pos;
61+
pos.x=0;
62+
pos.y=0;
63+
pos.h=image_surface->h;
64+
pos.w=image_surface->w;
65+
66+
// Multipliers for height and width. Used in the loop below to apply
67+
// the surface multiple times to help loop it
68+
uint8_t height_mult=0;
69+
uint8_t width_mult=0;
70+
71+
// loop surface
72+
while ( (height_mult*image_surface->h) < EngineRef.get_height() ){
73+
74+
// Reset width for every line
75+
width_mult=0;
76+
77+
// Set new line we're at using the multiplier
78+
pos.y=(height_mult*image_surface->h);
79+
80+
// Create all horizontal copies
81+
while ( width_mult*image_surface->w < EngineRef.get_width() ){
82+
83+
pos.x=(width_mult*image_surface->w);
84+
SDL_BlitSurface(image_surface, &size, temp_surface, &pos);
85+
86+
width_mult++;
87+
}
88+
89+
height_mult++;
90+
}
91+
92+
// Create the texture
93+
Texture = SDL_CreateTextureFromSurface(EngineRef.renderer, temp_surface);
94+
95+
96+
// Get Reference to engine obj
97+
ref = &EngineRef;
98+
99+
temp.x=0;
100+
temp.y=0;
101+
temp.w=ref->get_width();
102+
temp.h=ref->get_height();
103+
104+
// Clear surfaces from memory
105+
SDL_FreeSurface(image_surface);
106+
SDL_FreeSurface(temp_surface);
107+
108+
109+
}
110+
111+
112+
void background::destroy(void){
113+
SDL_DestroyTexture(Texture);
114+
}
115+

Diff for: gfx/colorkey.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef _COLORKEY_H
2+
#define _COLORKEY_H
3+
4+
/**
5+
* This function will colorkey an SDL surface and check its success
6+
* @param {bool} enable - Whether or not we're colorkeying this surface
7+
* @param {SDL_Surface* } - surface we're colorkeying
8+
* @return {bool} - whether or not we were successful in colorkeying (If enabled), will return true by default if disabled
9+
*/
10+
bool color_key_surface ( bool enable, SDL_Surface* surface){
11+
12+
// Check if we're even colorkeying anything
13+
if (enable){
14+
15+
// Colorkey and return result
16+
int result = SDL_SetColorKey(surface, SDL_TRUE, SDL_MapRGB(surface->format, 255, 0, 255));
17+
18+
// Checking result...
19+
if ( result != 0 ){
20+
// If we don't get a zero (Success) then...
21+
printf("Unable to colorkey surface!");
22+
return false;
23+
}
24+
}
25+
// If all else goes well, return true
26+
return true;
27+
}
28+
29+
#endif

0 commit comments

Comments
 (0)