#include "renderer.h" #include #include #include // för rand() #include #include "vecmath.h" #include "gameobject.h" #include "player.h" #include "background.h" const Uint8 *state; GameObject* gameObjects[] = {&background, &ship}; int nGameObjects = 2; void close(); void vandStrang(); void printToWindow(char* str, int x, int y); void printToConsole(char* str, int x, int y); int main( int argc, char* args[] ) { // If you want the program to not launch the terminal, then go to // Project->Settings->General->"This program is a GUI application" and uncheck that flag. // Start up SDL and create window of width=800, height = 600 initRenderer(800, 600); // Create an object ship.gfxObj = createGfxObject( "../ship.png" ); ship.gfxObj.outputWidth = 200; ship.gfxObj.outputHeight = 200; ship.pos.x = 400.0f; ship.pos.y = 300.0f; ship.speed = 3; //(Note: 90/360, without .0, are integer numbers and division then becomes equal to 0) ship.angle = 0; // unused ship.angleSpeed = 90.0/360.0; // unused ship.scale = 1.0f; ship.scaleSpeed = 0; // unused ship.update = updateShip; ship.render = render; // Create the background object background.gfxObj = createGfxObject( "../background.jpg" ); background.gfxObj.outputWidth = 800; background.gfxObj.outputHeight = 600; background.pos.x = 400.0f; background.pos.y = 300.0f; background.speed = 0; // unused background.angle = 0; //(Note: 1/100 are integer numbers and division becomes equal to 0) background.scale = 1.8f; background.scaleSpeed = 1.0/100.0; background.update = updateBackground; background.render = render; state = SDL_GetKeyboardState(NULL); // get pointer to key states void (*print) (char* str, int x, int y) = printToWindow; char strang[] = "Hello World!"; int loopIter = 0; while(true) // The real-time loop { // Handle events on the inqueue (e.g., mouse events) SDL_Event e; //Event handler while( SDL_PollEvent( &e ) != 0 ) { if( e.type == SDL_QUIT ) { //User requests quit close(); exit(0); } if( (e.type == SDL_KEYDOWN) && (e.key.keysym.scancode == SDL_SCANCODE_C) ) { static bool toggle = true; toggle = !toggle; if(!toggle) print = printToConsole; else print = printToWindow; } } // Clear screen with a grey background color, red=0x33, blue=0x33, green=0x33, alpha=0xff. 0=minimum, 0xff=maximum. // Alpha is transparency: 0 = fully transparent, 0xFF = fully opaque. However, actual use of transparency requires further settings. SDL_SetRenderDrawColor( gRenderer, 0x33, 0x33, 0x33, 0xFF ); SDL_RenderClear( gRenderer ); // Update our object(s) for(int i=0; i < nGameObjects; i++) gameObjects[i]->update(gameObjects[i]); // Render our object(s) - background objects first, and then forward objects for(int i=0; i < nGameObjects; i++) gameObjects[i]->render(gameObjects[i]); if( (loopIter % 100) == 99 ) vandStrang(strang); print(strang, 300, 150); // This function updates the screen and also sleeps ~16 ms or so (based on the screen's refresh rate), // because we used the flag SDL_RENDERER_PRESENTVSYNC in function initRenderer() SDL_RenderPresent( gRenderer ); SDL_Delay(15); // Fix for MacOS X Mojave, for which SDL's vsynch stopped working (as for 2019-01-01) loopIter++; } close(); //Free allocated resources return 0; } void vandStrang(char str[]) { int l = strlen(str); int half_len = l/2; for(int i=0; i