Skip to content

Commit

Permalink
Started input handling, implemented component rendering, fixed error …
Browse files Browse the repository at this point in the history
…logging
  • Loading branch information
gorbit99 committed Oct 15, 2019
1 parent 2b632ac commit 5e56ec8
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 3 deletions.
30 changes: 30 additions & 0 deletions Component.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Component.h"

#include "debugmalloc.h"

SDL_Surface *component_load_graphic(const char *path, float size, float thickness, TTF_Font *font) {

FILE *f = fopen(path, "r");
Expand Down Expand Up @@ -64,3 +66,31 @@ SDL_Surface *component_load_graphic(const char *path, float size, float thicknes

return component;
}

ComponentData component_create_data(float x, float y, SDL_Texture *texture, size_t outCount) {
ComponentData dat = {
x,
y,
0,
0,
texture,
outCount,
(bool *)calloc(outCount, sizeof(bool))
};
SDL_QueryTexture(texture, NULL, NULL, &dat.w, &dat.h);
return dat;
}

void component_free_data(ComponentData *dat) {
free(dat->out);
}

void component_render(ComponentData *dat, SDL_Renderer *renderer, Point camPos) {
SDL_Rect r = {
(int)(dat->x - camPos.x),
(int)(dat->y - camPos.y),
dat->w,
dat->h
};
SDL_RenderCopy(renderer, dat->texture, NULL, &r);
}
13 changes: 13 additions & 0 deletions Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@
#include "Utility.h"
#include "Graphics.h"

typedef struct ComponentData {
float x, y;
int w, h;
SDL_Texture *texture;
size_t outCount;
bool *out;
} ComponentData;

SDL_Surface *component_load_graphic(const char *path, float size, float thickness, TTF_Font *font);

ComponentData component_create_data(float x, float y, SDL_Texture *texture, size_t outCount);
void component_free_data(ComponentData *dat);

void component_render(ComponentData *dat, SDL_Renderer *renderer, Point camPos);

#endif //HOMEWORK_COMPONENT_H
2 changes: 2 additions & 0 deletions Graphics.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Graphics.h"

#include "debugmalloc.h"

#define set_pixel(dest, x, y, c) (((x)>=0&&(x)<(dest)->w&&(y)>=0&&(y)<(dest)->h)?((uint32_t *)(dest)->pixels)[(y) * (dest)->w + (x)] = (c):0)
#define int_to_color(color) ((SDL_Color){(color) >> 24u, ((color) >> 16u) & 0xffu, ((color) >> 8u) & 0xffu, (color) & 0xffu})

Expand Down
6 changes: 6 additions & 0 deletions Input.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "Input.h"
#include "debugmalloc.h"

void input_handle_event(SDL_Event &e) {

}
8 changes: 8 additions & 0 deletions Input.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef HOMEWORK_INPUT_H
#define HOMEWORK_INPUT_H

#include <SDL.h>

void input_handle_event(SDL_Event &e);

#endif //HOMEWORK_INPUT_H
4 changes: 3 additions & 1 deletion Utility.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Utility.h"

#include "debugmalloc.h"

SDL_Surface *get_rgba_surface(int w, int h) {
SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormat(0, w, h, 32, SDL_PIXELFORMAT_ABGR32);
return surface;
Expand Down Expand Up @@ -30,7 +32,7 @@ char *file_from_path(char *path) {
void log_error_base(const char *file, int line, const char *fmt, ...) {
va_list valist;
char *fileName = file_from_path((char *)file);
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Error in file %s on line %d:\n", fileName, line);
SDL_LogMessage(SDL_LOG_CATEGORY_ERROR, SDL_LOG_PRIORITY_CRITICAL, "Error in file %s on line %d:\n", fileName, line);
va_start(valist, fmt);
SDL_LogMessageV(SDL_LOG_CATEGORY_ERROR, SDL_LOG_PRIORITY_CRITICAL, fmt, valist);
va_end(valist);
Expand Down
2 changes: 2 additions & 0 deletions Window.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Window.h"

#include "debugmalloc.h"

void __log_output_function(void *unused, int category, SDL_LogPriority priority, const char *message) {
FILE *file = fopen("log.txt", "a");
time_t t = time(NULL);
Expand Down
4 changes: 4 additions & 0 deletions log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
----------2019-10-15 12:56:13----------
Test Hello
----------2019-10-15 13:02:58----------
Test Hello
4 changes: 2 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

int main(int argc, char **argv) {

debugmalloc_log_file("debugmalloclog.txt");

SDL_Window *window;
SDL_Renderer *renderer;

Expand Down Expand Up @@ -62,7 +64,5 @@ int main(int argc, char **argv) {
TTF_Quit();
SDL_Quit();

malloc(sizeof(int) * 20);

return 0;
}

0 comments on commit 5e56ec8

Please sign in to comment.