From d420f1aa35e0ca123be6c9f8793c17156aae9ff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Thu, 25 Oct 2018 20:31:53 +0200 Subject: [PATCH] Added argument parsing. Now also allows you to change resolution and fullscreen mode. --- src/args.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++ src/globals.h | 5 ++-- src/main.cpp | 27 +++++++++++++++--- src/simulation.cpp | 2 +- 4 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 src/args.h diff --git a/src/args.h b/src/args.h new file mode 100644 index 0000000..4a71c64 --- /dev/null +++ b/src/args.h @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2018 Ortega Froysa, Nicolás All rights reserved. + * Author: Ortega Froysa, Nicolás + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + */ + +#pragma once + +#include "globals.h" + +extern "C" { + +#include +#include + +const char *argp_program_version = VERSION; +const char *argp_program_bug_address = "nortega@themusicinnoise.net"; + +static char desc[] = "A psychedelic cube that changes color in 3D."; +static struct argp_option opts[] = { + { "fullscreen", 'f', 0, 0, "Set window to fullscreen", 0 }, + { "screen-width", 'w', "width", 0, "Set width of resolution", 0 }, + { "screen-height", 'h', "height", 0, "Set height of resolution", 0 }, + { 0, 0, 0, 0, 0, 0 } +}; + +struct args { + int fullscreen; + unsigned short width, height; +}; + +static error_t parse_opt(int key, char *arg, struct argp_state *state) { + struct args *args = (struct args*)state->input; + switch(key) + { + case 'f': + args->fullscreen = 1; + break; + case 'w': + args->width = (unsigned short)atoi(arg); + break; + case 'h': + args->height = (unsigned short) atoi(arg); + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; +} + +static struct argp argp = { opts, parse_opt, 0, desc, 0, 0, 0 }; +} diff --git a/src/globals.h b/src/globals.h index 71afa83..13517bd 100644 --- a/src/globals.h +++ b/src/globals.h @@ -31,10 +31,9 @@ # define VERSION "version" #endif -#define SCREEN_WIDTH 800 -#define SCREEN_HEIGHT 600 - extern "C" { +extern unsigned short screen_width; +extern unsigned short screen_height; extern SDL_Window *window; extern GLuint program_id; extern GLuint matrix_id; diff --git a/src/main.cpp b/src/main.cpp index 7562e8d..5b41f41 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,32 +24,45 @@ #include +#include #include #include #include -#include "globals.h" +#include "args.h" #include "shaders.hpp" #include "simulation.hpp" +unsigned short screen_width; +unsigned short screen_height; + SDL_Window *window; GLuint program_id; GLuint matrix_id; -int main() { +int main(int argc, char *argv[]) { + // handle arguments + struct args args = { 0, 800, 600 }; + + argp_parse(&argp, argc, argv, 0, 0, &args); + + screen_width = args.width; + screen_height = args.height; + if(SDL_Init(SDL_INIT_VIDEO) < 0) { std::cerr << "ERROR: " << SDL_GetError() << std::endl; exit(1); } + // setup SDL2 window 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); window = SDL_CreateWindow("Trippy Cube", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - SCREEN_WIDTH, SCREEN_HEIGHT, + args.width, args.height, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); if(not window) @@ -59,7 +72,13 @@ int main() { exit(1); } - //SDL_ShowCursor(SDL_DISABLE); + if(args.fullscreen) + { + SDL_SetWindowBordered(window, SDL_FALSE); + SDL_SetWindowFullscreen(window, + SDL_WINDOW_FULLSCREEN_DESKTOP); + } + SDL_SetRelativeMouseMode(SDL_TRUE); SDL_GLContext glcontext = SDL_GL_CreateContext(window); diff --git a/src/simulation.cpp b/src/simulation.cpp index 0c7364e..034d491 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -49,7 +49,7 @@ void run() { glm::mat4 mvp; { glm::mat4 proj = glm::perspective(glm::radians(45.0f), - static_cast(SCREEN_WIDTH) / static_cast(SCREEN_HEIGHT), + static_cast(screen_width) / static_cast(screen_height), 0.1f, 100.0f); glm::mat4 view = glm::lookAt(