From 32773a08eb11d286682ca8cad41171ce804f1631 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Wed, 31 May 2023 15:39:59 +0200 Subject: Works --- main.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 main.c (limited to 'main.c') diff --git a/main.c b/main.c new file mode 100644 index 0000000..82b3c07 --- /dev/null +++ b/main.c @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include + +#include "SDL2/SDL.h" +#include "SDL2/SDL2_gfxPrimitives.h" + +int main() { + int sock_in = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + struct sockaddr_in server_addr; + + SDL_Window *screen; + SDL_Renderer *renderer; + int screen_width = 448; + //int screen_height = 236; + int screen_height = 160; + uint8_t inbuf[screen_width * screen_height / 8 + 10]; + + if (SDL_Init(SDL_INIT_EVERYTHING) == -1) { + fprintf(stderr, "Can't initialize SDL.\n"); + exit(1); + } + screen = SDL_CreateWindow("Laserharfe", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_width, screen_height, SDL_WINDOW_OPENGL); + if (!screen) { + fprintf(stderr, "Can't set video mode.\n"); + exit(1); + } + renderer = SDL_CreateRenderer(screen, -1, 0); + + SDL_RenderClear(renderer); + + memset(&server_addr, 0, sizeof(server_addr)); + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(2342); + server_addr.sin_addr.s_addr = inet_addr("192.168.178.69"); + + if (bind(sock_in, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0){ + fprintf(stderr, "Couldn't bind to the port\n"); + exit(1); + } + + struct timeval tv; + memset(&tv, 0, sizeof(tv)); + tv.tv_usec = 30000; + setsockopt(sock_in, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + + while (1) { + SDL_Event ev; + SDL_PollEvent(&ev); + if (ev.type == SDL_QUIT) exit(0); + + size_t received = recvfrom(sock_in, inbuf, sizeof(inbuf), 0, NULL, 0); + if (received == -1) + continue; + + printf("Packet received, size: %zd\n", received); + SDL_RenderClear(renderer); + + for (int row = 0; row < screen_height; ++row) { + for (int col = 0; col < screen_width; ++col) { + uint8_t pixel = inbuf[10 + (row * screen_width + col) / 8]; + int on = pixel << (col & 7); + filledCircleColor(renderer, col, row, 1, (on & 0x80) ? 0xffffffff : 0); + } + } + SDL_RenderPresent(renderer); + } +} -- cgit v1.2.3