// SDL2_02 [Moving Rectangle].nova // Simple SDL2 example with moving rectangle. // Using namespace declarations. using namespace lib.emscripten; using namespace lib.sdl2; // The application class. class SDL2_02_Moving_Rectangle { // Static data members. private static SDL_Window window; private static SDL_Renderer renderer; private static SDL_Rect rect; // Application class's "main" function. public static void main( String[] args ) { Stream.writeLine( "SDL2_02 [Moving Rectangle].nova" ); // Disable the emscripten full-screen controls. Emscripten.disableControls( ); // Setup SDL. SDL2.SDL_Init( SDL2.SDL_INIT_VIDEO ); window = SDL2.SDL_CreateWindow( "SDL2_02 [Moving Rectangle]", SDL2.SDL_WINDOWPOS_CENTERED, SDL2.SDL_WINDOWPOS_CENTERED, 640, 480, SDL2.SDL_WINDOW_SHOWN ); // Check for a null reference. if ( window == null ) { // Output an error message. Stream.writeLine( "Failed to create window: " + SDL2.SDL_GetError( ) ); // Abort the application. return; } renderer = SDL2.SDL_CreateRenderer( window, -1, SDL2.SDL_RENDERER_ACCELERATED | SDL2.SDL_RENDERER_PRESENTVSYNC ); // Define the rectangle. rect = new SDL_Rect( -80, 100, 80, 80 ); // Check for the emscripten environment. if ( Emscripten.isActive( ) ) { int simulate_infinite_loop = 1; // Call the function repeatedly. int fps = -1; // Call the function as fast as the browser wants to render (typically 60fps). Emscripten.setMainLoop( renderFrame, fps, simulate_infinite_loop ); } else { // Rendering and event processing loop. do { SDL_Event e = SDL2.SDL_PollEvent( ); if ( e != null ) if ( e.id == SDL2.SDL_QUIT ) break; renderFrame( ); } while( true ); } // Shutdown app. SDL2.SDL_DestroyRenderer( renderer ); SDL2.SDL_DestroyWindow( window ); SDL2.SDL_Quit( ); } public static void renderFrame( ) { // Set the background colour (dark blue). SDL2.SDL_SetRenderDrawColor( renderer, 0, 0, 128, 255 ); // Clear the window. SDL2.SDL_RenderClear( renderer ); // Set the rectangle colour (orange). SDL2.SDL_SetRenderDrawColor( renderer, 255, 128, 0, 255 ); // Render the rectangle. SDL2.SDL_RenderFillRect( renderer, rect ); // Update the screen. SDL2.SDL_RenderPresent( renderer ); // Move the rectangle and wraparound if required. if ( ++rect.x > 640 ) rect.x = -80; } }