summaryrefslogtreecommitdiff
path: root/main.c
blob: 82b3c074500f83fbf54fd1b2aea4338e2a2cfc75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>

#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);
    }
}