#include "renderer.h" #include #include #include // för rand() GfxObject ship, background; void close(); void vandStrang(char str[]) { int l = strlen(str); int half_len = l/2; for(int i=0; i= shakeStop) // sätt bShake = false; if( bShake == false && ((rand() % 60)==1) ) { bShake = true; shakeStop = t + (rand() % 20) + 30; } if( bShake && t < shakeStop) { *x += 2* ((t % 3) - 1); *y += ((rand() % 3) - 1); } if( bShake && (t >= shakeStop) ) { bShake = false; } t++; } 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 = createGfxObject( "../ship.png" ); ship.outputWidth = 200; ship.outputHeight = 200; int x = 400, y = 300, speed = 3; //(Note: 90/360, without .0, are integer numbers and division then becomes equal to 0) double shipAngle = 0, shipAngleSpeed = 90.0/360.0; // Create the background object background = createGfxObject( "../background.jpg" ); background.outputWidth = 800; background.outputHeight = 600; double angle = 0; //(Note: 1/100 are integer numbers and division becomes equal to 0) float scale = 1.8f, scaleSpeed = 1.0/100.0; const Uint8 *state = SDL_GetKeyboardState(NULL); // get pointer to key states 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); } } // 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) shake(&x, &y); angle = fmod(angle+0.02, 360); scale += 1.0/2500.0; if (state[SDL_SCANCODE_RIGHT]) x = (x+speed >= 799) ? 799 : x+speed; if (state[SDL_SCANCODE_LEFT]) x = (x-speed <= 0) ? 0 : x-speed; if (state[SDL_SCANCODE_DOWN]) y = (y+speed >= 599) ? 599 : y+speed; if (state[SDL_SCANCODE_UP]) y = (y-speed <= 0) ? 0 : y-speed; if (state[SDL_SCANCODE_W]) scale = scale + scaleSpeed; if (state[SDL_SCANCODE_S]) scale = (scale - scaleSpeed <= 0) ? 0 : scale-scaleSpeed; if (state[SDL_SCANCODE_A]) shipAngle = fmod(shipAngle - shipAngleSpeed, 360.0); if (state[SDL_SCANCODE_D]) shipAngle = fmod(shipAngle + shipAngleSpeed, 360.0); // Render our object(s) - background objects first, and then forward objects (like a painter) renderGfxObject(&background, 400, 300, angle, scale); renderGfxObject(&ship, x, y, shipAngle, 1.0f); if( (loopIter % 100) == 99 ) vandStrang(strang); renderText(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 vsynch stopped working (as for 2019-01-01) loopIter++; } close(); //Free allocated resources return 0; } void close() { //Preferably, you should free all your GfxObjects, by calls to freeGfxObject(GfxObject* obj), but you don't have to. freeGfxObject(&ship); closeRenderer(); //Free resources and close SDL }