-
Notifications
You must be signed in to change notification settings - Fork 42
/
dgvideo_sdl2.c
92 lines (81 loc) · 2.53 KB
/
dgvideo_sdl2.c
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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <SDL2/SDL.h>
#include "dgvideo.h"
#include "dgvideo_render.h"
extern int sdl_live;
struct dgvideo_renderer {
struct dgvideo *dg;
SDL_Renderer *render;
SDL_Texture *texture;
SDL_Window *window;
};
void dgvideo_render(struct dgvideo_renderer *render)
{
SDL_Rect sr;
sr.x = 0;
sr.y = 0;
sr.w = 256;
sr.h = 128;
SDL_UpdateTexture(render->texture, NULL, dgvideo_get_raster(render->dg), 1024);
SDL_RenderClear(render->render);
SDL_RenderCopy(render->render, render->texture, NULL, &sr);
SDL_RenderPresent(render->render);
}
void dgvideo_renderer_free(struct dgvideo_renderer *render)
{
if (render->texture)
SDL_DestroyTexture(render->texture);
free(render);
}
struct dgvideo_renderer *dgvideo_renderer_create(struct dgvideo *dg)
{
struct dgvideo_renderer *render;
/* We will need a nicer way to do this once we have multiple SDL using
devices */
if (sdl_live == 0) {
sdl_live = 1;
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
fprintf(stderr, "SDL init failed: %s.\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
}
render = malloc(sizeof(struct dgvideo_renderer));
if (render == NULL) {
fprintf(stderr, "Out of memory.\n");
exit(1);
}
memset(render, 0, sizeof(struct dgvideo_renderer));
render->dg = dg;
render->window = SDL_CreateWindow("DGVideo",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
768, 384,
SDL_WINDOW_RESIZABLE);
if (render->window == NULL) {
fprintf(stderr, "Unable to create window: %s.\n", SDL_GetError());
exit(1);
}
render->render = SDL_CreateRenderer(render->window, -1, SDL_RENDERER_ACCELERATED);
if (render->render == NULL) {
fprintf(stderr, "Unable to create renderer: %s.\n", SDL_GetError());
exit(1);
}
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
SDL_RenderSetLogicalSize(render->render, 256, 128);
render->texture = SDL_CreateTexture(render->render,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
256, 128);
if (render->render == NULL) {
fprintf(stderr, "Unable to create renderer: %s.\n", SDL_GetError());
exit(1);
}
SDL_SetRenderDrawColor(render->render, 0, 0, 0, 255);
SDL_RenderClear(render->render);
SDL_RenderPresent(render->render);
return render;
}