/* draws a singel pixel directly to the screen */
-void p_draw_pixel(i32 x, i32 y, struct r_color color);
+void p_draw_pixel(i32 x, i32 y, r_color color);
+
+/* clears the current screen buffer */
+void p_draw_clear(void);
+
+/* inits the draw of a frame */
+void p_draw_begin(void);
+/* ends a frame draw */
+void p_draw_end(void);
+
+/* presents the current frame */
+void p_draw_present(void);
#endif
#include "../p_draw.h"
+#include "p_window.h"
+#include "../u_assert.h"
-void p_draw_pixel(i32 x, i32 y, struct r_color color) {
- LRTS_UNUSED(x);
- LRTS_UNUSED(y);
- LRTS_UNUSED(color);
+void p_draw_begin(void) {
+}
+
+void p_draw_end(void) {
+}
+
+void p_draw_clear(void) {
+ SDL_FillSurfaceRect(r_target, NULL, 0x000000FF);
+}
+
+void p_draw_pixel(i32 x, i32 y, r_color color) {
+ u32 *pixel = LRTS_NULL;
+ /* drop the draw if out of bounds */
+ if (x < 0 || x > r_target->w) {
+ return;
+ }
+ if (y < 0 || y > r_target->h) {
+ return;
+ }
+
+ pixel = (unsigned int*)(((char*)r_target->pixels) + r_target->pitch * y + x);
+ *pixel = color;
+}
+
+void p_draw_present(void) {
+ SDL_Surface* screen = SDL_GetWindowSurface(p_main_window);
+ SDL_BlitSurface(r_target, NULL, screen, NULL);
+ SDL_UpdateWindowSurface(p_main_window);
}
exit(-1);
}
- p_main_window = SDL_CreateWindow(lrts_cfg()->name, 640, 640, 0);
+ p_main_window = SDL_CreateWindow(lrts_cfg()->name, R_WIDTH, R_HEIGHT, 0);
if (p_main_window == LRTS_NULL) {
u_log(U_LOG_ERR, "Failed to create window: %s\n", SDL_GetError());
exit(-1);
}
+ r_target = SDL_CreateSurface(R_WIDTH, R_HEIGHT, SDL_PIXELFORMAT_RGBX8888);
+ if (r_target == LRTS_NULL) {
+ u_log(U_LOG_ERR, "Failed to create surface: %s\n", SDL_GetError());
+ exit(-1);
+ }
+
u_log(U_LOG_DEBUG, "Init successful\n");
return 0;
}
int p_renderer_finish(void) {
+ SDL_DestroySurface(r_target);
SDL_DestroyWindow(p_main_window);
SDL_Quit();
u_log(U_LOG_DEBUG, "Exiting...\n");
#include "../u_defs.h"
+SDL_Surface *r_target;
+
int p_poll_events() {
SDL_Event e;
while (SDL_PollEvent(&e)) {
extern SDL_Window *p_main_window;
+/* main surface to render to */
+extern SDL_Surface *r_target;
+
#endif
-struct r_color r_color(u8 r, u8 g, u8 b, u8 a) {
- struct r_color c;
- c.r = r;
- c.g = g;
- c.b = b;
- c.a = a;
- return c;
-}
#include "u_defs.h"
-struct r_color {
- u8 r;
- u8 g;
- u8 b;
- u8 a;
-};
-
-struct r_color r_color(u8 r, u8 g, u8 b, u8 a);
+typedef u32 r_color;
#endif
#include "p_draw.h"
void r_render_frame() {
- struct r_color c = r_color(0, 0, 0, 0xFF);
+ p_draw_begin();
+ p_draw_clear();
- p_draw_pixel(0, 0, c);
+ p_draw_pixel(0, 0, 0xFF0000FF);
+
+ p_draw_end();
+ p_draw_present();
}
#ifndef R_RENDER_H__
#define R_RENDER_H__
+/* internal rendering resolution */
+#define R_WIDTH 800
+#define R_HEIGHT 600
+
void r_render_frame();
#endif