72 lines
2.1 KiB
C
72 lines
2.1 KiB
C
//
|
|
// Created by william on 17/05/24.
|
|
//
|
|
|
|
#include <SDL.h>
|
|
#include "window.h"
|
|
#include "log.h"
|
|
|
|
NesWindow window_init(int width, int height, int scaling, char *title) {
|
|
NesWindow win;
|
|
win.scaling = scaling;
|
|
win.width = width * scaling;
|
|
win.height = height * scaling;
|
|
win.canvas = canvas_init(width, height);
|
|
|
|
int renderer_flags = SDL_RENDERER_ACCELERATED;
|
|
int window_flags = 0;
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
|
log_error("Couldn't initialize SDL: %s", SDL_GetError());
|
|
exit(-1);
|
|
}
|
|
|
|
win.window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, win.width, win.height,
|
|
window_flags);
|
|
if (!win.window) {
|
|
log_error("Failed to open %d x %d window: %s", win.width, win.height, SDL_GetError());
|
|
exit(-1);
|
|
}
|
|
|
|
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
|
|
|
|
win.renderer = SDL_CreateRenderer(win.window, -1, renderer_flags);
|
|
if (!win.renderer) {
|
|
log_error("Failed to create renderer: %s\n", SDL_GetError());
|
|
exit(-1);
|
|
}
|
|
|
|
return win;
|
|
}
|
|
|
|
void window_uninit(NesWindow *window) {
|
|
canvas_uninit(&window->canvas);
|
|
}
|
|
|
|
void window_render(NesWindow *window) {
|
|
SDL_RenderClear(window->renderer);
|
|
|
|
for (int y = 0; y < window->canvas.height; y++) {
|
|
for (int x = 0; x < window->canvas.width; x++) {
|
|
int pixel_index = x + y * window->canvas.width;
|
|
Pixel pixel = window->canvas.pixels[pixel_index];
|
|
|
|
SDL_SetRenderDrawColor(window->renderer, pixel.r, pixel.g, pixel.b, 255);
|
|
|
|
for (int i = 0; i < window->scaling; i++) {
|
|
for (int j = 0; j < window->scaling; j++) {
|
|
int scaled_x = x * window->scaling + i;
|
|
int scaled_y = y * window->scaling + j;
|
|
SDL_RenderDrawPoint(window->renderer, scaled_x, scaled_y);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void window_present(NesWindow *window) {
|
|
SDL_RenderPresent(window->renderer);
|
|
|
|
// TODO: Check if this is a good location
|
|
canvas_reset(&window->canvas);
|
|
} |