nesemu/gui/window.c

101 lines
3.1 KiB
C
Raw Normal View History

//
// Created by william on 17/05/24.
//
2024-05-17 13:16:21 -04:00
#include <SDL.h>
#include "window.h"
#include "log.h"
2024-06-16 19:22:40 -04:00
NesSdlContext window_init(char *title, int width, int height, int scale) {
NesSdlContext context;
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);
}
2024-06-16 19:22:40 -04:00
int actual_width = width * scale;
int actual_height = height * scale;
context.window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, actual_width,
actual_height, window_flags);
if (!context.window) {
log_error("Failed to open %d x %d SDL window: %s", actual_width, actual_height, SDL_GetError());
exit(-1);
}
2024-06-16 19:22:40 -04:00
context.renderer = SDL_CreateRenderer(context.window, -1, renderer_flags);
if (!context.renderer) {
log_error("Failed to create renderer: %s", SDL_GetError());
SDL_DestroyWindow(context.window);
exit(-1);
}
2024-06-16 19:22:40 -04:00
return context;
}
2024-06-16 19:22:40 -04:00
void window_uninit(NesSdlContext context) {
SDL_DestroyRenderer(context.renderer);
SDL_DestroyWindow(context.window);
}
2024-06-16 19:22:40 -04:00
//NesWindow window_init(int width, int height, char *title) {
// NesWindow win;
// win.width = width;
// win.height = 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 sdl_window: %s", win.width, win.height, SDL_GetError());
// exit(-1);
// }
//
// 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) {
// SDL_DestroyRenderer(window->renderer);
// SDL_DestroyWindow(window->window);
//}
//
//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);
//}