diff options
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 70 |
1 files changed, 70 insertions, 0 deletions
@@ -0,0 +1,70 @@ | |||
1 | #include <sys/socket.h> | ||
2 | #include <arpa/inet.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <stdint.h> | ||
5 | #include <stdio.h> | ||
6 | |||
7 | #include "SDL2/SDL.h" | ||
8 | #include "SDL2/SDL2_gfxPrimitives.h" | ||
9 | |||
10 | int main() { | ||
11 | int sock_in = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | ||
12 | struct sockaddr_in server_addr; | ||
13 | |||
14 | SDL_Window *screen; | ||
15 | SDL_Renderer *renderer; | ||
16 | int screen_width = 448; | ||
17 | //int screen_height = 236; | ||
18 | int screen_height = 160; | ||
19 | uint8_t inbuf[screen_width * screen_height / 8 + 10]; | ||
20 | |||
21 | if (SDL_Init(SDL_INIT_EVERYTHING) == -1) { | ||
22 | fprintf(stderr, "Can't initialize SDL.\n"); | ||
23 | exit(1); | ||
24 | } | ||
25 | screen = SDL_CreateWindow("Laserharfe", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_width, screen_height, SDL_WINDOW_OPENGL); | ||
26 | if (!screen) { | ||
27 | fprintf(stderr, "Can't set video mode.\n"); | ||
28 | exit(1); | ||
29 | } | ||
30 | renderer = SDL_CreateRenderer(screen, -1, 0); | ||
31 | |||
32 | SDL_RenderClear(renderer); | ||
33 | |||
34 | memset(&server_addr, 0, sizeof(server_addr)); | ||
35 | server_addr.sin_family = AF_INET; | ||
36 | server_addr.sin_port = htons(2342); | ||
37 | server_addr.sin_addr.s_addr = inet_addr("192.168.178.69"); | ||
38 | |||
39 | if (bind(sock_in, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0){ | ||
40 | fprintf(stderr, "Couldn't bind to the port\n"); | ||
41 | exit(1); | ||
42 | } | ||
43 | |||
44 | struct timeval tv; | ||
45 | memset(&tv, 0, sizeof(tv)); | ||
46 | tv.tv_usec = 30000; | ||
47 | setsockopt(sock_in, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); | ||
48 | |||
49 | while (1) { | ||
50 | SDL_Event ev; | ||
51 | SDL_PollEvent(&ev); | ||
52 | if (ev.type == SDL_QUIT) exit(0); | ||
53 | |||
54 | size_t received = recvfrom(sock_in, inbuf, sizeof(inbuf), 0, NULL, 0); | ||
55 | if (received == -1) | ||
56 | continue; | ||
57 | |||
58 | printf("Packet received, size: %zd\n", received); | ||
59 | SDL_RenderClear(renderer); | ||
60 | |||
61 | for (int row = 0; row < screen_height; ++row) { | ||
62 | for (int col = 0; col < screen_width; ++col) { | ||
63 | uint8_t pixel = inbuf[10 + (row * screen_width + col) / 8]; | ||
64 | int on = pixel << (col & 7); | ||
65 | filledCircleColor(renderer, col, row, 1, (on & 0x80) ? 0xffffffff : 0); | ||
66 | } | ||
67 | } | ||
68 | SDL_RenderPresent(renderer); | ||
69 | } | ||
70 | } | ||