summaryrefslogtreecommitdiff
path: root/src/screen.c
blob: 5e23e0ba5983939fc9c8b29225dec099ee20bb54 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "screen.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

void init_screen(Screen* screen, const char* title, uint16_t width, uint16_t height) {
    screen->width = width;
    screen->height = height;
    size_t pixel_count = screen->width * screen->height;
    screen->pixels = malloc(pixel_count * sizeof(uint32_t));
    memset(screen->pixels, 0, pixel_count * sizeof(uint32_t));

	SDL_Init(SDL_INIT_VIDEO);

    screen->window =
        SDL_CreateWindow(
            title,
            SDL_WINDOWPOS_CENTERED_DISPLAY(0),
            SDL_WINDOWPOS_CENTERED_DISPLAY(0),
            screen->width,
            screen->height,
            0
        );

    if (!screen->window) {
		printf("fatal: failed to create sdl window\n");
        exit(EXIT_FAILURE);
	}

    screen->renderer = 
        SDL_CreateRenderer(
            screen->window,
            -1,
            SDL_RENDERER_ACCELERATED
            | SDL_RENDERER_PRESENTVSYNC
        );

    if (!screen->renderer) {
        printf("fatal: failed to create sdl renderer\n");
        exit(EXIT_FAILURE);
    }

    screen->texture =
        SDL_CreateTexture(
            screen->renderer,
            SDL_PIXELFORMAT_RGBA8888,
            SDL_TEXTUREACCESS_STREAMING,
            screen->width,
            screen->height
        );
    
    if (!screen->texture) {
        printf("fatal: failed to create window texture\n");
        exit(EXIT_FAILURE);
    }

    screen->open = true;
    screen->delta = 0;
}

static long last = 0;
void poll_screen(Screen* screen) {
    struct timespec spec;
    clock_gettime(CLOCK_MONOTONIC, &spec);
    long now = round(spec.tv_nsec / 1.0e6) + spec.tv_sec * 1000;
    if (last == 0) {
        screen->delta = 0;
    } else {
        screen->delta = (now - last) / 1000.0;
    }
    last = now;
    
    SDL_Event ev;
    while (SDL_PollEvent(&ev)) {
        switch (ev.type) {
            case SDL_QUIT:
                screen->open = false;
                break;
            default:
                break;
        }
    }

    screen->key_state = SDL_GetKeyboardState(NULL);
}

void draw_screen(Screen* screen) {
    void *px;
    int pitch;
    SDL_LockTexture(screen->texture, NULL, &px, &pitch);
    {
        for (uint16_t y = 0; y < screen->height; y++) {
            memcpy(
                &((uint8_t*) px)[y * pitch],
                &screen->pixels[y * screen->width],
                screen->width * sizeof(uint32_t)
            );
        }
    }
    SDL_UnlockTexture(screen->texture);

    SDL_SetRenderTarget(screen->renderer, NULL);
    SDL_SetRenderDrawColor(screen->renderer, 0, 0, 0, 0xFF);
    SDL_SetRenderDrawBlendMode(screen->renderer, SDL_BLENDMODE_NONE);

    SDL_RenderClear(screen->renderer);
    SDL_RenderCopyEx(
        screen->renderer,
        screen->texture,
        NULL,
        NULL,
        0.0,
        NULL,
        SDL_FLIP_VERTICAL);

    SDL_RenderCopy(screen->renderer, NULL, NULL, &((SDL_Rect) { 0, 0, 512, 512 }));
    SDL_RenderPresent(screen->renderer);
}

void free_screen(Screen* screen) {
    SDL_DestroyTexture(screen->texture);
    SDL_DestroyRenderer(screen->renderer);
	SDL_DestroyWindow(screen->window);
}

bool key_pressed(Screen* screen, SDL_Scancode code) {
    return screen->key_state[code & 0xFFFF];    
}