chipd8/source/window.d

101 lines
2.5 KiB
D
Raw Normal View History

2018-06-22 23:54:39 +01:00
module window;
import derelict.sdl2.sdl;
import glad.gl.all;
/**
* Converts an integer representing a colour, for example 0x428bca into a 4 element
* int array for passing to OpenGL.
*/
GLfloat[4] to(T : GLfloat[4])(int color, ubyte alpha = 255) nothrow @nogc pure {
GLfloat[4] gl_color = [ //mask out r, g, b components from int
cast(float)cast(ubyte)(color>>16)/255,
cast(float)cast(ubyte)(color>>8)/255,
cast(float)cast(ubyte)(color)/255,
cast(float)cast(ubyte)(alpha)/255
];
return gl_color;
} // to!GLfloat[4]
struct Window {
import core.stdc.stdio;
import core.stdc.stdlib;
SDL_Window* window;
SDL_GLContext context;
void createWindow(int w, int h, const char* title = "Chipd8 Emu") {
2018-06-22 23:54:39 +01:00
assert(w > 0, "window width must be > 0");
assert(h > 0, "window height must be > 0");
// minimum OpenGL 3.3
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);
SDL_Window* new_win = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_OPENGL);
if (new_win == null) {
printf("SDL2 - Window could not be created: %s\n", SDL_GetError());
return;
}
SDL_GLContext new_con = SDL_GL_CreateContext(new_win);
if (new_con == null) {
printf("SDL2 - failed creating OpenGL 3.3 context: %s\n", SDL_GetError());
return;
}
import glad.gl.loader;
import std.functional;
// Check OpenGL properties
printf("OpenGL loaded\n");
gladLoadGL((const (char)* a) => SDL_GL_GetProcAddress(a));
printf("Vendor: %s\n", glGetString(GL_VENDOR));
printf("Renderer: %s\n", glGetString(GL_RENDERER));
printf("Version: %s\n", glGetString(GL_VERSION));
2018-06-22 23:54:39 +01:00
// assign em yes
window = new_win;
context = new_con;
atexit(SDL_Quit);
} // createWindow
2018-06-22 23:54:39 +01:00
void handleEvent(ref SDL_Event ev) {
2018-06-22 23:54:39 +01:00
} // handleEvent
2018-06-22 23:54:39 +01:00
void renderClear(int colour) {
2018-06-22 23:54:39 +01:00
auto col = to!(float[4])(colour, 255);
2018-06-22 23:54:39 +01:00
glClearColor(col[0], col[1], col[2], col[3]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
} // renderClear
2018-06-22 23:54:39 +01:00
void renderPresent() {
2018-06-22 23:54:39 +01:00
SDL_GL_SwapWindow(window);
} // renderPresent
2018-06-22 23:54:39 +01:00
void mouse_pos(ref int x, ref int y) {
SDL_GetMouseState(&x, &y);
}
2018-06-22 23:54:39 +01:00
void window_size(ref int w, ref int h) {
SDL_GetWindowSize(window, &w, &h);
}
2018-06-22 23:54:39 +01:00
void framebuffer_size(ref int w, ref int h) {
SDL_GL_GetDrawableSize(window, &w, &h);
}
2018-06-22 23:54:39 +01:00
} // Window