// Workshop 3 part 1 import sdljava.*; import sdljava.video.*; import sdljava.event.*; class s03_p01 { protected static SDLSurface screen; // We will call this function whenever we want to exit the program public static void exit(int value) { SDLMain.quit(); System.out.println("Exiting.."); System.exit(value); } // In this function, we retrieve and handle events public static boolean handle_events() { try { // SDL stores events in a queue. // We can either wait for events, in which case the function we call will not return until there is an event, // or we can have a quick look at the event queue, using a function that will retrieve an event if there is one, returning null if there isn't. // In an interactive game, it's not much use if we get stuck waiting for events, as we won't be able to draw or update the scene. // Therefore, we want to use the 'quick look' option. // This is called polling for events. // The waiting technique is known as blocking. // Poll for events SDLEvent event = SDLEvent.pollEvent(); // If there was an event.. if( event != null ) // What we do depends on the type switch( event.getType() ) { case SDLEvent.SDL_QUIT: // This event type is generated when the user clicks on the 'x' to close the window return true; default: } } catch(SDLException e) { System.err.println("Error while polling event : " + SDLMain.getError()); exit(1); } return false; } // In this function, we update our scene public static void update() { } // In this function, we draw our scene public static void draw() { } public static void main(String[] args) { // Create an SDL object SDLMain sdl = new SDLMain(); // SDL uses exceptions when it encounters problems try { // Initialise SDL to use its video (graphics) capabilities sdl.init(SDLMain.SDL_INIT_VIDEO); // SDL uses what it calls a 'surface' as a window. screen = SDLVideo.setVideoMode(640, 480, 0, 0 ); } catch(SDLException e) // Catch SDL problems { // We shall handle any problems by printing a stack trace: e.printStackTrace(); // and an explanation if that wasn't enough System.err.println("SDL could not initialise"); exit(1); } // This will store whether or not the user has asked to quit boolean quit = false; // This is our main "game loop", and we will run it // while we've not been asked to quit... while( quit == false ) { // When the user presses a key, or uses the mouse, that input is stored in SDL as an 'event'. // As an interactive program, we need to look at these events and decide what to do about them. // Our function here also returns true if we want to quit: quit = handle_events(); // In an interactive program, be it a game or whatever, we usually have a variety of things to update each frame. // These could be character animations, physics simulations, explosions, whatever we want... update(); // Finally, we need to draw our scene draw(); } exit(0); } }