#include #include #include #include #include #include #include #include #include #include #include #include #include #include "nexus.h" char *nexus_version_string = "0.1.8"; int verbose; int mainloopend; int init_done; SDL_DisplayMode display_mode; SDL_Window *window; SDL_GLContext context; GLfloat winX = 100.0, winY = 40.0, winW = 800.0, winH = 600.0; char window_title[1024]; unsigned int fps; time_t t0, tprev; struct timeval tv0, tv_prev, tv_diff; char *fps_text; static const struct option long_options[] = { {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'V'}, {"verbose", no_argument, NULL, 'v'}, {"floor-factor", required_argument, NULL, 'f'}, {"position-x", required_argument, NULL, 'X'}, {"position-y", required_argument, NULL, 'Y'}, {"width", required_argument, NULL, 'W'}, {"height", required_argument, NULL, 'H'}, {NULL, 0, NULL, 0} }; static const char *short_options = "hVvf:X:Y:W:H:"; void NexusExit(void) { // Prevent saving zeroes if exit() was called before // the score was loaded. if (init_done) { if (verbose) printf("Saving element score\n"); ElementScoreSave(); } if (verbose) printf("\nnexus exited\n"); } void tvdiff(struct timeval *tv_start, struct timeval *tv_end, struct timeval *tv_diff2) { tv_diff2->tv_sec = tv_end->tv_sec - tv_start->tv_sec; if (tv_start->tv_sec == tv_end->tv_sec) { tv_diff2->tv_usec = tv_end->tv_usec - tv_start->tv_usec; } else { tv_diff2->tv_usec = tv_end->tv_usec + (1000000-tv_start->tv_usec); if (tv_diff2->tv_usec >= 1000000) { ++tv_diff2->tv_sec; tv_diff2->tv_usec -= 1000000; } if (tv_diff2->tv_sec > 0) --tv_diff2->tv_sec; } } void ShowHelp(void) { printf("Options: -H/--help | -V/--version | -v/--verbose | -f/--floor-factor NUM |\n" " -X/--position-x PIXELS | -Y/--position-y PIXELS |\n" " -W/--width PIXELS | -H/--height PIXELS\n"); } void ShowVersion(void) { printf("nexus %s\n", nexus_version_string); } int main(int argc, char **argv) { // Process program arguments int c; while (1) { c = getopt_long(argc, argv, short_options, long_options, NULL); if (c == -1) break; switch (c) { case 'h': ShowHelp(); exit(0); case 'V': ShowVersion(); exit(0); case 'v': verbose = 1; break; case 'f': if (optarg != NULL) floor_factor = atoi(optarg); if (floor_factor <= 0) floor_factor = 1; break; case 'X': if (optarg != NULL) winX = atoi(optarg); break; case 'Y': if (optarg != NULL) winY = atoi(optarg); break; case 'W': if (optarg != NULL) winW = atoi(optarg); break; case 'H': if (optarg != NULL) winH = atoi(optarg); break; default: printf("nexus error: Unknown argument: %c (%d)\n", (char)c, c); break; } } if (verbose) printf("nexus %s started\n", nexus_version_string); atexit(NexusExit); if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf ("nexus: SDL_Init() failed.\n"); exit(1); } int major, minor; SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major); SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor); if (verbose) printf("Using OpenGL %d.%d\n", major, minor); sprintf(window_title, "nexus %s", nexus_version_string); window = SDL_CreateWindow(window_title, winX, winY, winW, winH, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); context = SDL_GL_CreateContext(window); if (context == NULL) { printf("nexus: Cannot create SDL2/GL context. Error: %s\n", SDL_GetError()); SDL_Quit(); exit(1); } // Needs a context if (verbose) printf("OpenGL %s installed\n", glGetString(GL_VERSION)); glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { printf ("nexus: Cannot initialize GLEW.\n"); SDL_GL_DeleteContext(context); SDL_Quit(); exit(1); } fps_text = malloc(20); sprintf(fps_text, "0 fps"); ModeSet(MODE_MAIN); EventInit(); DeltaInit(); CameraInit(); FontInit(); // There must be no display list creation before this call RenderInit(); FloorInit(); ElementInit(); FlagInit(); SkyInit(); MoonInit(); BrowserInit(); MemoryInit(); init_done = 1; tprev = time(NULL); gettimeofday(&tv_prev, NULL); if (verbose) printf("Mainloop started\n"); while (!mainloopend) { ++fps; EventFunc(); DeltaFunc(); if (cam.moving) CameraMove(); if (goto_enabled) CameraGotoMove(); if (render) RenderFunc(); } SDL_GL_DeleteContext(context); SDL_Quit(); return 0; }