1#include "cottonwindow.h"
2#include <stdio.h>
3
4int
5cottonwindow_init (CottonWindow *cw, const char *title, int window_w, int window_h, int texture_w, int texture_h)
6{
7
8 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
9 fprintf(stderr, "cotton couldn't start SDL :( : %s\n", SDL_GetError());
10 return 0;
11 }
12
13 cw->window = SDL_CreateWindow(title, 0, 0, window_w, window_h, SDL_WINDOW_SHOWN);
14
15 if (!cw->window) {
16 fprintf(stderr, "cotton couldn't create a window :( : %s\n", SDL_GetError());
17 return 0;
18 }
19
20 cw->renderer = SDL_CreateRenderer(cw->window, -1, 0);
21 if (!cw->renderer) {
22 fprintf(stderr, "cotton couldn't render this :( : %s\n", SDL_GetError());
23 return 0;
24 }
25
26 cw->texture =
27 SDL_CreateTexture(cw->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, texture_w, texture_h);
28 if (!cw->texture) {
29 fprintf(stderr, "cotton couldn't apply textures :( : %s\n", SDL_GetError());
30 return 0;
31 }
32
33 return 1;
34}
35
36void
37cottonwindow_update(CottonWindow *cw, const void *buffer, int pitch)
38{
39 SDL_UpdateTexture(cw->texture, NULL, buffer, pitch);
40 SDL_RenderClear(cw->renderer);
41 SDL_RenderCopy(cw->renderer, cw->texture, NULL, NULL);
42 SDL_RenderPresent(cw->renderer);
43}
44
45int cottonwindow_process_input
46(CottonWindow *cw, uint8_t *keys)
47{
48 (void)cw;
49 (void)keys;
50 int quit = 0;
51 SDL_Event event;
52
53 while (SDL_PollEvent(&event)) {
54 switch (event.type) {
55 case SDL_QUIT: {
56 quit = 1;
57 break;
58 }
59
60 case SDL_KEYDOWN: {
61 if (event.key.keysym.sym == SDLK_ESCAPE)
62 quit = 1;
63 break;
64 }
65 }
66 }
67
68 return quit;
69}
70
71void cottonwindow_destroy
72(CottonWindow *cw)
73{
74 SDL_DestroyTexture(cw->texture);
75 SDL_DestroyRenderer(cw->renderer);
76 SDL_DestroyWindow(cw->window);
77 SDL_Quit();
78}