Create main.cpp
This commit is contained in:
216
src/main.cpp
Normal file
216
src/main.cpp
Normal file
@@ -0,0 +1,216 @@
|
||||
/*
|
||||
|
||||
Minceraft
|
||||
|
||||
By CobaltXII
|
||||
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// GLAD is used as the loader for OpenGL functions. On operating systems such
|
||||
// as OS X, a loader is not necessarily needed. However, on most other
|
||||
// systems, a loader is required in order to use any OpenGL functions.
|
||||
|
||||
#include <glad/glad.h>
|
||||
|
||||
// SDL2 is used as the multimedia interface for Minceraft. It provides
|
||||
// cross-platform functions that allow easy access to the operating system.
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
// The entry point.
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
// Initialize SDL.
|
||||
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING))
|
||||
{
|
||||
std::cout << "Could not initialize SDL." << std::endl;
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// The dimensions of the SDL_Window*.
|
||||
|
||||
int sdl_x_res = 840;
|
||||
int sdl_y_res = 525;
|
||||
|
||||
// Create the SDL_Window*.
|
||||
|
||||
SDL_Window* sdl_window = SDL_CreateWindow
|
||||
(
|
||||
"Minceraft 0.0.0",
|
||||
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
|
||||
sdl_x_res,
|
||||
sdl_y_res,
|
||||
|
||||
SDL_WINDOW_OPENGL
|
||||
);
|
||||
|
||||
// Make sure the SDL_Window* was created successfully.
|
||||
|
||||
if (!sdl_window)
|
||||
{
|
||||
std::cout << "Could not create the SDL_Window*." << std::endl;
|
||||
|
||||
exit(2);
|
||||
}
|
||||
|
||||
// Request OpenGL 3.3 (core profile).
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
|
||||
// Request double-buffering.
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
|
||||
// Request a 24-bit depth buffer. 24-bit depth buffers are supported on
|
||||
// most modern hardware. Having a floating-point precision of lower than
|
||||
// 24-bits will cause graphical issues.
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
||||
|
||||
// Enable vertical retrace synchronization. If this is not enabled, a
|
||||
// phenomenon known as tearing occurs, which causes half of the screen to
|
||||
// retain the contents of the last frame instead of the entire screen
|
||||
// updating immediately.
|
||||
|
||||
SDL_GL_SetSwapInterval(1);
|
||||
|
||||
// Create the SDL_GLContext.
|
||||
|
||||
SDL_GLContext gl_context = SDL_GL_CreateContext(sdl_window);
|
||||
|
||||
if (!gl_context)
|
||||
{
|
||||
std::cout << "Could not create the SDL_GLContext." << std::endl;
|
||||
|
||||
exit(3);
|
||||
}
|
||||
|
||||
// Load all OpenGL 3.3 (core profile) functions using GLAD.
|
||||
|
||||
if (!gladLoadGLLoader(SDL_GL_GetProcAddress))
|
||||
{
|
||||
std::cout << "Could not load OpenGL functions using GLAD." << std::endl;
|
||||
|
||||
exit(4);
|
||||
}
|
||||
|
||||
// Make sure the OpenGL version that was provided is greater than or equal
|
||||
// to OpenGL 3.3.
|
||||
|
||||
if (GLVersion.major * 10 + GLVersion.minor < 33)
|
||||
{
|
||||
std::cout << "Could not load OpenGL 3.3 using GLAD. Received OpenGL " << GLVersion.major << "." << GLVersion.minor << " instead." << std::endl;
|
||||
|
||||
exit(5);
|
||||
}
|
||||
|
||||
// Create variables to represent the position of the mouse pointer, and
|
||||
// state of the mouse buttons.
|
||||
|
||||
int sdl_mouse_x = 0;
|
||||
int sdl_mouse_y = 0;
|
||||
|
||||
bool sdl_mouse_l = false;
|
||||
bool sdl_mouse_r = false;
|
||||
|
||||
// Enter the main loop.
|
||||
|
||||
bool sdl_running = true;
|
||||
|
||||
while (sdl_running)
|
||||
{
|
||||
// Poll events.
|
||||
|
||||
SDL_Event e;
|
||||
|
||||
while (SDL_PollEvent(&e))
|
||||
{
|
||||
if (e.type == SDL_QUIT)
|
||||
{
|
||||
// The application was quit.
|
||||
|
||||
sdl_running = false;
|
||||
}
|
||||
else if (e.type == SDL_MOUSEMOTION)
|
||||
{
|
||||
// The mouse moved.
|
||||
|
||||
sdl_mouse_x = e.motion.x;
|
||||
sdl_mouse_y = e.motion.y;
|
||||
}
|
||||
else if (e.type == SDL_MOUSEBUTTONDOWN)
|
||||
{
|
||||
// A mouse button was pressed.
|
||||
|
||||
if (e.button.button == SDL_BUTTON_LEFT)
|
||||
{
|
||||
sdl_mouse_l = true;
|
||||
}
|
||||
else if (e.button.button == SDL_BUTTON_RIGHT)
|
||||
{
|
||||
sdl_mouse_r = true;
|
||||
}
|
||||
}
|
||||
else if (e.type == SDL_MOUSEBUTTONUP)
|
||||
{
|
||||
// A mouse button was released.
|
||||
|
||||
if (e.button.button == SDL_BUTTON_LEFT)
|
||||
{
|
||||
sdl_mouse_l = false;
|
||||
}
|
||||
else if (e.button.button == SDL_BUTTON_RIGHT)
|
||||
{
|
||||
sdl_mouse_r = false;
|
||||
}
|
||||
}
|
||||
else if (e.type == SDL_KEYDOWN)
|
||||
{
|
||||
// A key was pressed.
|
||||
|
||||
SDL_Keycode key = e.key.keysym.sym;
|
||||
|
||||
if (key == SDLK_ESCAPE)
|
||||
{
|
||||
sdl_running = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clear the OpenGL context.
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// Swap the back buffer to the front, so that the last frame will
|
||||
// appear on the screen.
|
||||
|
||||
SDL_GL_SwapWindow(sdl_window);
|
||||
}
|
||||
|
||||
// Destroy all OpenGL related objects.
|
||||
|
||||
SDL_GL_DeleteContext(gl_context);
|
||||
|
||||
// Destroy all SDL related objects.
|
||||
|
||||
SDL_DestroyWindow(sdl_window);
|
||||
|
||||
SDL_Quit();
|
||||
|
||||
// Exit cleanly.
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
Reference in New Issue
Block a user