Skip to content

Commit

Permalink
update wayland software rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ColleagueRiley committed Jan 12, 2025
1 parent 0c9480a commit 1c3f879
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 123 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ endif

ifeq ($(RGFW_WAYLAND),1)
NO_VULKAN = 1
LIBS = -D RGFW_WAYLAND relative-pointer-unstable-v1-client-protocol.c xdg-decoration-unstable-v1.c xdg-shell.c -lwayland-cursor -lwayland-client -lEGL -lxkbcommon -lGL -lwayland-egl
NO_GLES = 0
NO_OSMESA = 0
LIBS = -D RGFW_WAYLAND relative-pointer-unstable-v1-client-protocol.c xdg-decoration-unstable-v1.c xdg-shell.c -lwayland-cursor -lwayland-client -lxkbcommon
LINK_GL1 = -lEGL -lGL -lwayland-egl

endif

LINK_GL3 =
Expand Down
35 changes: 11 additions & 24 deletions RGFW.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define RGFW_WAYLAND
/*
*
* RGFW 1.5-dev
Expand Down Expand Up @@ -93,7 +94,7 @@ int main() {
RGFW_window_swapBuffers(win);
glClearColor(0xFF, 0XFF, 0xFF, 0xFF);
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
}
Expand Down Expand Up @@ -4889,20 +4890,7 @@ static void wl_surface_frame_done(void *data, struct wl_callback *cb, uint32_t t
RGFW_window* win = (RGFW_window*)data;
if ((win->_flags & RGFW_NO_CPU_RENDER))
return;

/*#ifndef RGFW_X11_DONT_CONVERT_BGR
u32 x, y;
for (y = 0; y < (u32)win->r.h; y++) {
for (x = 0; x < (u32)win->r.w; x++) {
u32 index = (y * 4 * win->r.w) + x * 4;
u8 red = win->buffer[index];
win->buffer[index] = win->buffer[index + 2];
win->buffer[index + 2] = red;
}
}
#endif*/

wl_surface_attach(win->src.surface, win->src.wl_buffer, 0, 0);
wl_surface_damage_buffer(win->src.surface, 0, 0, win->r.w, win->r.h);
wl_surface_commit(win->src.surface);
Expand Down Expand Up @@ -4975,15 +4963,15 @@ void RGFW_init_buffer(RGFW_window* win) {
u8 color[] = {0x00, 0x00, 0x00, 0xFF};

size_t i;
for (i = 0; i < size; i += 4) {
for (i = 0; i < RGFW_bufferSize.w * RGFW_bufferSize.h * 4; i += 4) {
RGFW_MEMCPY(&win->buffer[i], color, 4);
}

RGFW_MEMCPY(win->src.buffer, win->buffer, RGFW_bufferSize.w * RGFW_bufferSize.h * 4);
RGFW_MEMCPY(win->src.buffer, win->buffer, win->r.w * win->r.h * 4);

#if defined(RGFW_OSMESA)
win->src.ctx = OSMesaCreateContext(OSMESA_RGBA, NULL);
OSMesaMakeCurrent(win->src.ctx, win->src.buffer, GL_UNSIGNED_BYTE, win->r.w, win->r.h);
win->src.ctx = OSMesaCreateContext(OSMESA_BGRA, NULL);
OSMesaMakeCurrent(win->src.ctx, win->buffer, GL_UNSIGNED_BYTE, RGFW_bufferSize.w, RGFW_bufferSize.h);
#endif
#else
RGFW_UNUSED(win);
Expand Down Expand Up @@ -5332,8 +5320,8 @@ void RGFW_window_swapBuffers(RGFW_window* win) {
RGFW_ASSERT(win != NULL);

/* clear the window*/
#ifdef RGFW_BUFFER
#ifndef RGFW_X11_DONT_CONVERT_BGR
#if defined(RGFW_BUFFER) || defined(RGFW_OSMESA)
#if !defined(RGFW_X11_DONT_CONVERT_BGR) && !defined(RGFW_OSMESA)
u32 x, y;
for (y = 0; y < (u32)win->r.h; y++) {
for (x = 0; x < (u32)win->r.w; x++) {
Expand All @@ -5345,9 +5333,8 @@ void RGFW_window_swapBuffers(RGFW_window* win) {

}
}
#endif
RGFW_MEMCPY(win->src.buffer, win->buffer, RGFW_bufferSize.w * RGFW_bufferSize.h * 4);

#endif
RGFW_MEMCPY(win->src.buffer, win->buffer, win->r.w * win->r.h * 4);
wl_surface_frame_done(win, NULL, 0);
if (!(win->_flags & RGFW_NO_GPU_RENDER))
#endif
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void drawLoop(RGFW_window *w) {
RGFW_window_makeCurrent(w);

#ifndef RGFW_VULKAN
glClearColor(255, 255, 255, 255);
glClearColor(1, 1, 1, 1);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

Expand Down
185 changes: 92 additions & 93 deletions examples/buffer/buffer.c
Original file line number Diff line number Diff line change
@@ -1,93 +1,92 @@
#define RGFW_IMPLEMENTATION
#define RGFW_BUFFER
//#define RGFW_OPENGL

#include "RGFW.h"

u8 icon[4 * 3 * 3] = {0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF};

RGFW_area screenSize;

/* fill buffer with a color, clearing anything that was on it */
void clear(RGFW_window* win, u8 color[4]) {
/* if all the values are the same */
if (color[0] == color[1] && color[0] == color[2] && color[0] == color[3]) {
/* set it all in one function */
memset(win->buffer, 0, screenSize.w * win->r.h * 4 * sizeof(u8));
return;
}

/* else we'll have to something more complex... */

/* loop through each *pixel* (not channel) of the buffer */
u32 x, y;
for (y = 0; y < (u32)win->r.h; y++) {
for (x = 0; x < screenSize.w; x++) {
u32 index = (y * 4 * screenSize.w) + x * 4;

/* copy the color to that pixel */
memcpy(win->buffer + index, color, 4 * sizeof(u8));
}
}
}

/* this can also be used to convert BGR to RGB */
void bitmap_rgbToBgr(u8* bitmap, RGFW_area a) {
/* loop through each *pixel* (not channel) of the buffer */
u32 x, y;
for (y = 0; y < a.h; y++) {
for (x = 0; x < a.w; x++) {
u32 index = (y * 4 * a.w) + x * 4;

u8 red = bitmap[index];
bitmap[index] = bitmap[index + 2];
bitmap[index + 2] = red;
}
}
}

void drawBitmap(RGFW_window* win, u8* bitmap, RGFW_rect rect) {
u32 y;
for (y = 0; y < (u32)rect.h; y++) {
u32 index = (rect.y + y) * (4 * screenSize.w) + rect.x * 4;
memcpy(win->buffer + index, bitmap + (4 * rect.w * y), rect.w * 4 * sizeof(u8));
}
}

void drawRect(RGFW_window* win, RGFW_rect r, u8 color[4]) {
for(int x = r.x; x < (r.x + r.w); x++) {
for(int y = r.y; y < (r.y + r.h); y++) {
int index = y * (4 * screenSize.w) + x * 4;

memcpy(win->buffer + index, color, 4 * sizeof(u8));
}
}
}

int main(void) {
RGFW_window* win = RGFW_createWindow("Basic buffer example", RGFW_RECT(0, 0, 500, 500), RGFW_windowCenter | RGFW_windowTransparent);

screenSize = RGFW_getScreenSize();

i8 running = 1;
while (running) {
while (RGFW_window_checkEvent(win)) {
if (win->event.type == RGFW_quit || RGFW_isPressed(win, RGFW_escape)) {
running = 0;
break;
}
}

clear(win, (u8[4]){0, 0, 255, 100});
drawRect(win, RGFW_RECT(200, 200, 200, 200), (u8[4]){255, 0, 0, 255});

drawBitmap(win, icon, RGFW_RECT(100, 100, 3, 3));

//RGFW_window_setGPURender(win, 0);
RGFW_window_swapBuffers(win);
RGFW_window_checkFPS(win, 0);
}

RGFW_window_close(win);
}

#define RGFW_IMPLEMENTATION
#define RGFW_BUFFER
//#define RGFW_OPENGL

#include "RGFW.h"

u8 icon[4 * 3 * 3] = {0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF};

RGFW_area screenSize;

/* fill buffer with a color, clearing anything that was on it */
void clear(RGFW_window* win, u8 color[4]) {
/* if all the values are the same */
if (color[0] == color[1] && color[0] == color[2] && color[0] == color[3]) {
/* set it all in one function */
memset(win->buffer, 0, screenSize.w * win->r.h * 4 * sizeof(u8));
return;
}

/* else we'll have to something more complex... */

/* loop through each *pixel* (not channel) of the buffer */
u32 x, y;
for (y = 0; y < (u32)win->r.h; y++) {
for (x = 0; x < screenSize.w; x++) {
u32 index = (y * 4 * screenSize.w) + x * 4;

/* copy the color to that pixel */
memcpy(win->buffer + index, color, 4 * sizeof(u8));
}
}
}

/* this can also be used to convert BGR to RGB */
void bitmap_rgbToBgr(u8* bitmap, RGFW_area a) {
/* loop through each *pixel* (not channel) of the buffer */
u32 x, y;
for (y = 0; y < a.h; y++) {
for (x = 0; x < a.w; x++) {
u32 index = (y * 4 * a.w) + x * 4;

u8 red = bitmap[index];
bitmap[index] = bitmap[index + 2];
bitmap[index + 2] = red;
}
}
}

void drawBitmap(RGFW_window* win, u8* bitmap, RGFW_rect rect) {
u32 y;
for (y = 0; y < (u32)rect.h; y++) {
u32 index = (rect.y + y) * (4 * screenSize.w) + rect.x * 4;
memcpy(win->buffer + index, bitmap + (4 * rect.w * y), rect.w * 4 * sizeof(u8));
}
}

void drawRect(RGFW_window* win, RGFW_rect r, u8 color[4]) {
for(int x = r.x; x < (r.x + r.w); x++) {
for(int y = r.y; y < (r.y + r.h); y++) {
int index = y * (4 * screenSize.w) + x * 4;

memcpy(win->buffer + index, color, 4 * sizeof(u8));
}
}
}

int main(void) {
RGFW_window* win = RGFW_createWindow("Basic buffer example", RGFW_RECT(0, 0, 500, 500), RGFW_windowCenter | RGFW_windowTransparent);

screenSize = RGFW_getScreenSize();

i8 running = 1;
while (running) {
while (RGFW_window_checkEvent(win)) {
if (win->event.type == RGFW_quit || RGFW_isPressed(win, RGFW_escape)) {
running = 0;
break;
}
}

clear(win, (u8[4]){0, 0, 255, 100});
drawRect(win, RGFW_RECT(200, 200, 200, 200), (u8[4]){255, 0, 0, 255});

drawBitmap(win, icon, RGFW_RECT(100, 100, 3, 3));

//RGFW_window_setGPURender(win, 0);
RGFW_window_swapBuffers(win);
RGFW_window_checkFPS(win, 0);
}

RGFW_window_close(win);
}
30 changes: 27 additions & 3 deletions examples/osmesa/osmesa.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,42 @@
#include "RGFW.h"
#include <stdio.h>

RGFW_area screenSize;
void clear(RGFW_window* win, u8 color[4]) {
/* if all the values are the same */
if (color[0] == color[1] && color[0] == color[2] && color[0] == color[3]) {
/* set it all in one function */
memset(win->buffer, 0, screenSize.w * win->r.h * 4 * sizeof(u8));
return;
}

/* else we'll have to something more complex... */

/* loop through each *pixel* (not channel) of the buffer */
u32 x, y;
for (y = 0; y < (u32)win->r.h; y++) {
for (x = 0; x < screenSize.w; x++) {
u32 index = (y * 4 * screenSize.w) + x * 4;

/* copy the color to that pixel */
memcpy(win->buffer + index, color, 4 * sizeof(u8));
}
}
}

int main(void) {
RGFW_setClassName("RGFW Basic");
RGFW_window* win = RGFW_createWindow("RGFW Example Window", RGFW_RECT(500, 500, 500, 500), RGFW_windowAllowDND | RGFW_windowCenter);
RGFW_window_makeCurrent(win);

screenSize = RGFW_getScreenSize();

while (RGFW_window_shouldClose(win) == RGFW_FALSE) {
RGFW_window_eventWait(win, RGFW_eventWaitNext);
while (RGFW_window_checkEvent(win) != NULL);

RGFW_window_makeCurrent(win);

glViewport(0, 0, win->r.w, win->r.h);
glClearColor(255, 255, 255, 255);
glClearColor(1, 1, 1, 1);

glClear(GL_COLOR_BUFFER_BIT);

Expand All @@ -28,6 +51,7 @@ int main(void) {
glEnd();

glFlush();

RGFW_window_swapBuffers(win);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/performance-checker/pc.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ RGFWDEF void print_timeUnit(void);
int main(void) {
RGFW_window* win = RGFW_createWindow("name", RGFW_RECT(500, 500, 500, 500), (u64)RGFW_CENTER);

glClearColor(0xFF, 0XFF, 0xFF, 0xFF);
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);


Expand Down

0 comments on commit 1c3f879

Please sign in to comment.