put everything in a class
This commit is contained in:
@@ -12,13 +12,13 @@ LIB_OBJ := $(LIB_SRC:.c=.o)
|
||||
|
||||
.PHONY: all clean
|
||||
|
||||
all: libwaymini.a example waymini
|
||||
all: libwaymini.a test waymini
|
||||
|
||||
libwaymini.a: $(LIB_OBJ)
|
||||
ar rcs $@ $^
|
||||
|
||||
example: example.c libwaymini.a
|
||||
$(CC) $(CFLAGS) -o $@ example.c -L. -lwaymini $(LDFLAGS)
|
||||
test: test.cpp libwaymini.a
|
||||
g++ $(CFLAGS) -o $@ test.cpp -L. -lwaymini $(LDFLAGS)
|
||||
|
||||
waymini: waymini_standalone.c waymini.c xdg-shell-client-protocol.c
|
||||
$(CC) $(CFLAGS) -o $@ waymini_standalone.c waymini.c xdg-shell-client-protocol.c $(LDFLAGS)
|
||||
@@ -27,7 +27,7 @@ waymini: waymini_standalone.c waymini.c xdg-shell-client-protocol.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
rm -f $(LIB_OBJ) libwaymini.a example waymini
|
||||
rm -f $(LIB_OBJ) libwaymini.a test waymini
|
||||
|
||||
xdg-shell-client-protocol.c xdg-shell-client-protocol.h: gen-protocols.sh
|
||||
bash gen-protocols.sh
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
/* Example program that links against the waymini library.
|
||||
* No external dependencies beyond waymini itself and the C standard library.
|
||||
*/
|
||||
|
||||
#include "waymini.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
static void on_key(void *user, uint32_t keycode, uint32_t state) {
|
||||
(void)user;
|
||||
const char *label = state ? "down" : "up ";
|
||||
printf("key %s: %u\n", label, keycode);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
namespace UwU {
|
||||
class Draw {
|
||||
public:
|
||||
struct waymini* wm;
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
uint32_t stride;
|
||||
uint8_t *pixels;
|
||||
|
||||
Draw(uint32_t width, uint32_t height, waymini_key_cb on_key) {
|
||||
this->wm = waymini_create(width, height, on_key, NULL);
|
||||
if (!this->wm) exit(1);
|
||||
|
||||
this->width = waymini_get_width(this->wm);
|
||||
this->height = waymini_get_height(this->wm);
|
||||
this->stride = waymini_get_stride(this->wm);
|
||||
printf("surface: %ux%u stride=%u\n", this->width, this->height, this->stride);
|
||||
|
||||
this->pixels = (uint8_t *)waymini_get_pixels(this->wm);
|
||||
if (!this->pixels) {
|
||||
waymini_destroy(this->wm);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Draw a rectangle (duh)
|
||||
void rectangle(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint8_t r, uint8_t g, uint8_t b) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Fill a simple gradient. Each row is stride bytes.
|
||||
void rainbow() {
|
||||
for (uint32_t y = 0; y < this->height; y++) {
|
||||
for (uint32_t x = 0; x < this->width; x++) {
|
||||
size_t i = (size_t)y * waymini_get_stride(this->wm) + (size_t)x * 4;
|
||||
this->pixels[i + 0] = (uint8_t)(y * 255 / this->height); /* B */
|
||||
this->pixels[i + 1] = 0x00; /* G */
|
||||
this->pixels[i + 2] = (uint8_t)(x * 255 / this->width); /* R */
|
||||
this->pixels[i + 3] = 0xFF; /* A (ignored for XRGB) */
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
uint32_t w = 800, h = 480;
|
||||
|
||||
// struct waymini *wm = waymini_create(w, h, on_key, NULL);
|
||||
UwU::Draw draw = UwU::Draw(w, h, on_key);
|
||||
|
||||
draw.rainbow();
|
||||
|
||||
waymini_present(draw.wm);
|
||||
draw.pixels[200001] = 255;
|
||||
|
||||
while (!waymini_should_close(draw.wm)) {
|
||||
if (waymini_dispatch(draw.wm) < 0) break;
|
||||
}
|
||||
|
||||
waymini_destroy(draw.wm);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user