alistair
5 years ago
commit
fb99b1fec9
9 changed files with 403 additions and 0 deletions
@ -0,0 +1,23 @@
@@ -0,0 +1,23 @@
|
||||
CXX = gcc
|
||||
# Update these paths to match your installation
|
||||
# You may also need to update the linker option rpath, which sets where to look for
|
||||
# the SDL2 libraries at runtime to match your install
|
||||
SDL_INCLUDE = -lSDL2 -lSDL2_image
|
||||
# You may need to change -std=c++11 to -std=c++0x if your compiler is a bit older
|
||||
CXXFLAGS = $(SDL_INCLUDE)
|
||||
EXE = main
|
||||
|
||||
all: $(EXE) |
||||
|
||||
$(EXE): main.o |
||||
$(CXX) $(CXXFLAGS) $< -o $@
|
||||
|
||||
main.o: main.c |
||||
|
||||
|
||||
clean: |
||||
|
||||
rm *.o && rm $(EXE)
|
||||
|
||||
|
||||
|
Binary file not shown.
@ -0,0 +1,101 @@
@@ -0,0 +1,101 @@
|
||||
|
||||
#include <stdio.h> |
||||
#include <time.h> |
||||
#include <string.h> |
||||
|
||||
// #define LOGGING 1
|
||||
// #define log_var(string, content) if (LOGGING) {time_t current_time; time(¤t_time); printf("%s",asctime(localtime(¤t_time))); printf(string, content);}
|
||||
// #define log_str(string) if (LOGGING) {time_t current_time; time(¤t_time); puts(asctime(localtime(¤t_time))); puts(string);}
|
||||
|
||||
void enumerate_string(char test_array[]) { |
||||
char *ptr; |
||||
ptr = &(test_array[0]); |
||||
for (int i=0; i<5; i++) { |
||||
printf("%c", *(ptr + i)); |
||||
} |
||||
} |
||||
|
||||
int copy_str(char * out_string, char * string) { |
||||
/* A drop in replacement for strcpy() from <string.h>*/ |
||||
int len = 0; |
||||
while (*(string + len) != '\0') { |
||||
*(out_string + len) = *(string + len); |
||||
len++; |
||||
} |
||||
*(out_string + len) = '\0'; |
||||
return (1); |
||||
} |
||||
|
||||
int get_str_len(char * string) { |
||||
int len = 0; |
||||
while (*(string + len) != '\0') { |
||||
len++; |
||||
} |
||||
return(len); |
||||
} |
||||
|
||||
void replace_char(char * array, char * charac) { |
||||
int length; |
||||
length = sizeof(*array)/sizeof(char); |
||||
char combined[length]; |
||||
|
||||
int i = 0; |
||||
int counter = 0; |
||||
|
||||
while (*(array + i) != '\0') { |
||||
if (*(array + i) != *charac) { |
||||
*(array + counter) = *(array + i); |
||||
counter++; |
||||
} |
||||
i++; |
||||
} |
||||
*(array + counter + 1) = '\0'; |
||||
} |
||||
|
||||
void join_strings(char * return_val, char * str_1, char * str_2) { |
||||
|
||||
int len1 = get_str_len(str_1); |
||||
int len2 = get_str_len(str_2); |
||||
|
||||
int length = len1 + len2; |
||||
char combined[length+1]; |
||||
int i = 0; |
||||
int j = 0; |
||||
|
||||
while (*(str_1 + i) != '\0') { |
||||
combined[i] = *(str_1 + i); |
||||
i++; |
||||
} |
||||
|
||||
while (*(str_2 + j) != '\0') { |
||||
combined[len1+j] = *(str_2 + j); |
||||
j++; |
||||
} |
||||
copy_str(return_val, combined); |
||||
} |
||||
|
||||
void get_time(void) { |
||||
char time_str[100]; |
||||
time_t current_time; |
||||
time(¤t_time);
|
||||
copy_str(time_str, asctime(localtime(¤t_time))); |
||||
|
||||
strcat(time_str, "hello world"); |
||||
//replace_char(&(time_str[0]), "\n");
|
||||
printf("%s", time_str); |
||||
|
||||
char string[20]; |
||||
copy_str(string, "NONEWLINEhello"); |
||||
|
||||
|
||||
replace_char(&(time_str[0]), "\n"); |
||||
char newstr[120]; |
||||
join_strings(&(newstr[0]), &(time_str[0]), &(string[0])); |
||||
replace_char(&(newstr[0]), "\n"); |
||||
|
||||
printf("%s", newstr); |
||||
|
||||
} |
||||
|
||||
|
||||
|
After Width: | Height: | Size: 117 KiB |
Binary file not shown.
@ -0,0 +1,279 @@
@@ -0,0 +1,279 @@
|
||||
|
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <SDL2/SDL.h> |
||||
#include <SDL2/SDL_image.h> |
||||
#include <string.h> |
||||
#include <time.h> |
||||
#include "basic-lib.c" |
||||
|
||||
const int screen_width = 800; |
||||
const int screen_height = 600; |
||||
|
||||
/* logging definition */ |
||||
|
||||
typedef enum { |
||||
// high-low priority
|
||||
// low-high verbosity
|
||||
SILENT, ERROR, WARN, INFO, DEBUG |
||||
} logger_level; |
||||
|
||||
logger_level LOGLEVEL = DEBUG; |
||||
logger_level STDOUTLEVEL = DEBUG; |
||||
|
||||
void logwrite(logger_level level, char message[]) { |
||||
// write a string message
|
||||
char time_str[100]; |
||||
time_t current_time; |
||||
time(¤t_time);
|
||||
|
||||
copy_str(time_str, asctime(localtime(¤t_time))); |
||||
|
||||
// delete the \n
|
||||
time_str[24] = ' '; |
||||
|
||||
char prepend[15]; |
||||
copy_str(prepend, time_str); |
||||
|
||||
|
||||
switch (level) { |
||||
case DEBUG: |
||||
strcat(prepend, "DEBUG"); |
||||
break; |
||||
case INFO: |
||||
strcat(prepend, "INFO "); |
||||
break; |
||||
case WARN: |
||||
strcat(prepend, "WARN "); |
||||
break; |
||||
case ERROR: |
||||
strcat(prepend, "ERROR"); |
||||
break; |
||||
} |
||||
|
||||
strcat(prepend, ": "); |
||||
|
||||
if (level <= LOGLEVEL) { |
||||
FILE * logfile = fopen("debug.log", "a"); |
||||
fprintf(logfile, "%s", prepend); |
||||
fprintf(logfile, message); |
||||
fclose(logfile); |
||||
} |
||||
|
||||
if (level <= STDOUTLEVEL) { |
||||
printf("%s", prepend); |
||||
printf(message); |
||||
} |
||||
|
||||
} |
||||
/* Type definition for object cleanups */ |
||||
typedef enum { |
||||
NO, SURFACE, TEXTURE, RENDERER, WINDOW |
||||
} sdl_types; |
||||
|
||||
typedef union { |
||||
SDL_Window * window; |
||||
SDL_Renderer * renderer; |
||||
SDL_Surface * surface; |
||||
SDL_Texture * texture; |
||||
void * generic; |
||||
} thing_for_cleanup ; |
||||
|
||||
typedef struct { |
||||
sdl_types kind; |
||||
thing_for_cleanup thing; |
||||
} sdl_abstract; |
||||
|
||||
static sdl_abstract cleanup_queue[100]; |
||||
static int cleanup_queue_length = 0; |
||||
|
||||
|
||||
void queue_for_cleanup(void * SDL_thing, sdl_types kind) { |
||||
sdl_abstract obj; |
||||
obj.kind = kind; |
||||
switch (kind) { |
||||
case TEXTURE: |
||||
obj.thing.texture = SDL_thing; |
||||
break; |
||||
case WINDOW: |
||||
obj.thing.window = SDL_thing; |
||||
break; |
||||
case RENDERER: |
||||
obj.thing.renderer = SDL_thing; |
||||
break; |
||||
case SURFACE: |
||||
obj.thing.surface = SDL_thing; |
||||
break; |
||||
|
||||
} |
||||
|
||||
cleanup_queue_length++;
|
||||
cleanup_queue[cleanup_queue_length] = obj; |
||||
} |
||||
|
||||
void cleanup(void * thing, sdl_types thing_type) { |
||||
if (!thing) { |
||||
logwrite(DEBUG, "Null passed to cleanup\n"); |
||||
return; |
||||
} |
||||
switch(thing_type) { |
||||
case SURFACE: |
||||
SDL_FreeSurface(thing); |
||||
logwrite(DEBUG, "Freed surface\n"); |
||||
break; |
||||
case TEXTURE: |
||||
SDL_DestroyTexture(thing); |
||||
logwrite(DEBUG, "Destroyed texture\n"); |
||||
break; |
||||
case RENDERER: |
||||
SDL_DestroyRenderer(thing); |
||||
logwrite(DEBUG, "Destroyed renderer\n"); |
||||
break; |
||||
case WINDOW: |
||||
SDL_DestroyWindow(thing); |
||||
logwrite(DEBUG, "Destroyed window\n"); |
||||
break; |
||||
case NO: |
||||
return; |
||||
} |
||||
} |
||||
|
||||
void empty_cleanup_queue(void) { |
||||
for (; cleanup_queue_length > 0; cleanup_queue_length--) { |
||||
sdl_abstract thing = cleanup_queue[cleanup_queue_length]; |
||||
cleanup(thing.thing.generic, thing.kind); |
||||
} |
||||
} |
||||
|
||||
|
||||
struct SDL_Window* make_window(void) { |
||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
|
||||
printf("error initializing SDL: %s\n", SDL_GetError());
|
||||
}
|
||||
return SDL_CreateWindow("sdl_tester",
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
screen_width, screen_height, 0);
|
||||
} |
||||
|
||||
void step(void) { |
||||
// game
|
||||
//
|
||||
// draw
|
||||
return;
|
||||
} |
||||
|
||||
void render_texture_at(struct SDL_Renderer * ren, struct SDL_Texture * texture,int x, int y) { |
||||
|
||||
SDL_Rect destination; |
||||
destination.x = x; |
||||
destination.y = y; |
||||
|
||||
SDL_QueryTexture(texture, NULL, NULL, &destination.w, &destination.h); |
||||
SDL_RenderCopy(ren, texture, NULL, &destination); |
||||
|
||||
// queue for cleanup
|
||||
} |
||||
|
||||
|
||||
void queue_object_for_cleanup(void * thing, sdl_types thing_type) { |
||||
|
||||
} |
||||
|
||||
void refresh(struct SDL_Renderer * ren) { |
||||
|
||||
} |
||||
|
||||
|
||||
SDL_Texture * load_image(SDL_Renderer * ren, char fname[]) { |
||||
SDL_Surface * surface = IMG_Load(fname) ; |
||||
if(!surface) { |
||||
printf("IMG_Load: %s\n", IMG_GetError()); |
||||
// handle error
|
||||
} |
||||
|
||||
SDL_Texture * png_image = SDL_CreateTextureFromSurface(ren, surface); |
||||
cleanup(surface, SURFACE); |
||||
return (png_image); |
||||
} |
||||
|
||||
|
||||
void draw_pictures(struct SDL_Renderer * ren) { |
||||
// display an initial splash screen
|
||||
|
||||
const char file[] = "image.bmp"; |
||||
SDL_Surface* surface = SDL_LoadBMP(file); |
||||
SDL_Texture * texture = SDL_CreateTextureFromSurface(ren,surface); |
||||
|
||||
const char png_file[] = "trans.PNG";
|
||||
|
||||
SDL_Surface * png_surface = IMG_Load(png_file) ; |
||||
if(!png_surface) { |
||||
printf("IMG_Load: %s\n", IMG_GetError()); |
||||
// handle error
|
||||
} |
||||
|
||||
SDL_Texture * png_image = SDL_CreateTextureFromSurface(ren, png_surface); |
||||
|
||||
// remove the surface from memory as no longer needed
|
||||
cleanup(png_surface, SURFACE); |
||||
cleanup(surface, SURFACE); |
||||
// check this actually does stuff
|
||||
|
||||
SDL_RenderClear(ren); |
||||
//render_texture_at(ren, texture, 100, 100);
|
||||
//render_texture_at(ren, png_image, 100, 100);
|
||||
SDL_RenderCopy(ren, png_image, NULL, NULL); |
||||
|
||||
// update the actual screen
|
||||
SDL_RenderPresent(ren); |
||||
|
||||
|
||||
} |
||||
|
||||
|
||||
int main() { |
||||
logwrite(INFO, "Starting\n"); |
||||
SDL_Window * win = make_window(); |
||||
SDL_Renderer * ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED); |
||||
queue_for_cleanup(win, WINDOW); |
||||
queue_for_cleanup(ren, RENDERER); |
||||
|
||||
IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG); |
||||
|
||||
if (ren == NULL) { |
||||
SDL_DestroyWindow(win); |
||||
SDL_Quit(); |
||||
} |
||||
|
||||
int close = 0; |
||||
int first = 1; |
||||
|
||||
while (!close) { |
||||
SDL_Event event;
|
||||
|
||||
while (SDL_PollEvent(&event)) { |
||||
switch (event.type) { |
||||
case SDL_QUIT: |
||||
close = 1; |
||||
break; |
||||
default: |
||||
if (!first) { |
||||
step(); |
||||
} else { |
||||
draw_pictures(ren); |
||||
first = 1; |
||||
} |
||||
|
||||
} |
||||
} |
||||
} |
||||
|
||||
printf("Cleanup queue length: %d\n", cleanup_queue_length); |
||||
empty_cleanup_queue(); |
||||
printf("Cleanup queue length: %d\n", cleanup_queue_length); |
||||
logwrite(DEBUG, "Emptied cleanup queue\n"); |
||||
return 0;
|
||||
}
|
||||
|
Binary file not shown.
After Width: | Height: | Size: 65 KiB |
After Width: | Height: | Size: 23 KiB |
Loading…
Reference in new issue