// Workshop 5 part 1 import sdljava.*; import sdljava.video.*; import sdljava.event.*; import java.util.LinkedList; import java.util.ListIterator; // This is a general purpose vector class class c_vector { // Constructor that takes another vector as input public c_vector( c_vector value ) { x = value.x; y = value.y; } // Constructor that takes two floats as input public c_vector( float a, float b ) { x = a; y = b; } // Constructor that takes one float as input public c_vector( float value ) { x = y = value; } // Constructor that takes no input public c_vector() { x = y = 0.0f; } // Normalises the vector public void normalise() { // Retrieve the magnitude float len = get_length(); if( len > 0.0f ) { // Normalise the vector by dividing the components by this magnitude x /= len; y /= len; } else // This should stop divide by zero errors x = y = 0.0f; } // Returns the length of the vector public float get_length() { // Return the magnitude (length) using Pythagoras' theorem return (float) Math.sqrt( (x*x) + (y*y) ); } // Our vector's components public float x, y; } class game_object { // Static function for storing the screen size public static void set_screen_size(c_vector screenIn) { // This will be used in the update functions, so see if the object collides with the screen screen = screenIn; } public game_object() { position = new c_vector(); size = new c_vector(); direction = new c_vector(); speed = 0; image = null; blacklist = false; } // Adds the object to the blacklist (i.e. flags that it should be removed) public void blacklist_add() { blacklist = true; } // Queries whether the object is on the blacklist public boolean blacklist_get() { return blacklist; } // For setting the movement direction of the object public void set_direction(c_vector in) { // The direction vector is a normalised unit vector // We don't know what the vector we've been given is, so we will normalise it in.normalise(); direction = in; } public boolean update(float deltaT_s) { // Update the position position.x += deltaT_s * direction.x * speed; position.y += deltaT_s * direction.y * speed; // Test against the sides of the screen, keeping track of whether it hits boolean hits_sides = false; if( position.x - (size.x/2) < 0.0f ) { position.x = size.x/2; hits_sides = true; } else if( position.x + (size.x/2) > screen.x ) { position.x = screen.x - (size.x/2); hits_sides = true; } if( position.y - (size.y/2) < 0.0f ) { position.y = size.y/2; hits_sides = true; } else if( position.y + (size.y/2) > screen.y ) { position.y = screen.y - (size.y/2); hits_sides = true; } // Return whether the object hits the sides of the screen return hits_sides; } public void draw(SDLSurface screen) throws SDLException { // Draw the object SDLRect rectangle = new SDLRect((int)position.x-(int)(size.x/2.0f),(int)position.y-(int)(size.y/2.0f),(int)size.x,(int)size.y); image.blitSurface(screen,rectangle); } protected c_vector position; protected c_vector size; protected c_vector direction; protected float speed; protected SDLSurface image; protected static c_vector screen; protected boolean blacklist; } class bullet extends game_object { // Bullet constructor // Java note: the no-arg constructor of the super class will be automatically called before this one. public bullet( game_object originIn, SDLSurface imageIn, c_vector directionIn, c_vector start_pos, float speedIn ) { image = imageIn; size.x = image.getWidth(); size.y = image.getHeight(); direction = directionIn; position = start_pos; speed = speedIn; origin = originIn; } // The bullet's update function public boolean update(float deltaT_s) { // For a bullet, we need to do something very slightly different for the update // Perform the regular update, making a note of whether we hit the sides of the screen boolean hits_sides = super.update(deltaT_s); // If we do hit the sides, we will be going off the screen, so need to be flagged for removal: if( hits_sides ) blacklist_add(); // Still return whether we hit the sides, just for completeness ;) return hits_sides; } // The game object that fired the bullet // This will be useful when we check for collisions, as we won't test against the object that fired it game_object origin; } class spaceship extends game_object { // Space ship constructor // Java note: the no-arg constructor of the super class will be automatically called before this one. public spaceship(SDLSurface imageIn, c_vector start_pos, float speedIn, c_vector shoot_directionIn, SDLSurface bullet_imageIn, float bullet_speedIn) { image = imageIn; // Determine our size from the image we have been given size.x = image.getWidth(); size.y = image.getHeight(); position = start_pos; shoot_direction = shoot_directionIn; bullet_image = bullet_imageIn; bullet_speed = bullet_speedIn; speed = speedIn; } // This function is called whenever we want the space ship to fire a bullet public bullet fire() { // Create and return a new bullet return new bullet((game_object)this,bullet_image,shoot_direction,new c_vector(position), bullet_speed ); } // The direction that new bullets will fire in c_vector shoot_direction; // The image to use for new bullets SDLSurface bullet_image; // The speed of a new bullet: float bullet_speed; } class s05_p01 { // Our SDL surface for drawing on static SDLSurface screen; // The player controllable space ship static spaceship my_ship; // The direction that the player controllable ship is moving in according to the user input static c_vector ship_direction; // List that contains all the game objects in our game static LinkedList<game_object> game_objects; // We will call this function whenever we want to exit the program. // It uninitialises SDL and forces a program exit. 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; case SDLEvent.SDL_KEYDOWN: // The user has pressed a key { // This means that the event is actually of a sub-type called an SDLKeyboardEvent. // If we cast the event to this type, we can retrieve the actual key type int key = ((SDLKeyboardEvent) event).getSym(); // What we do now depends on the key that was pressed switch( key ) { case SDLKey.SDLK_ESCAPE: exit(0); case SDLKey.SDLK_UP: ship_direction.y += -1.0f; my_ship.set_direction(new c_vector(ship_direction)); break; case SDLKey.SDLK_DOWN: ship_direction.y += 1.0f; my_ship.set_direction(new c_vector(ship_direction)); break; case SDLKey.SDLK_LEFT: ship_direction.x += -1.0f; my_ship.set_direction(new c_vector(ship_direction)); break; case SDLKey.SDLK_RIGHT: ship_direction.x += 1.0f; my_ship.set_direction(new c_vector(ship_direction)); break; case SDLKey.SDLK_SPACE: // This will be the fire button // We call our space ship's fire function // This returns a bullet, which is a game object // To get the game object updated, we need to add it to our list // Since this list dictates the drawing order too, we will add it to the front of the list // Since our ship is at the back, the bullets will be drawn under it. game_objects.addFirst((game_object)my_ship.fire()); break; default: } } break; case SDLEvent.SDL_KEYUP: // The user has released a key { // This means that the event is actually of a sub-type called an SDLKeyboardEvent. // If we cast the event to this type, we can retrieve the actual key type int key = ((SDLKeyboardEvent) event).getSym(); // What we do now depends on the key that was pressed switch( key ) { case SDLKey.SDLK_UP: ship_direction.y -= -1.0f; my_ship.set_direction(new c_vector(ship_direction)); break; case SDLKey.SDLK_DOWN: ship_direction.y -= 1.0f; my_ship.set_direction(new c_vector(ship_direction)); break; case SDLKey.SDLK_LEFT: ship_direction.x -= -1.0f; my_ship.set_direction(new c_vector(ship_direction)); break; case SDLKey.SDLK_RIGHT: ship_direction.x -= 1.0f; my_ship.set_direction(new c_vector(ship_direction)); break; } } break; 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( float deltaT_s ) { // Generate an iterator for the first item in the list // (Iterators are useful helper classes that allow us to cycle through a list. // We generate one that starts at the beginning of the list, // and we will use it to retrieve the next item) ListIterator it = game_objects.listIterator(0); // Go through the list while( it.hasNext() ) { // Retrieve the next item game_object current = (game_object) it.next(); // Update the object current.update(deltaT_s); // If an object needs to be deleted, it will set an internal 'blacklist' flag // If this flag is set, we need to remove it from our main game objects list. // Why might an object need to be deleted? // There are various reasons, for example: if the object explodes, or if a bullet reaches the edge of the screen if( current.blacklist_get() ) it.remove(); } } // In this function, we draw our scene // Upon encountering an error, this function throws an SDLException, which is passed on from SDL function calls public static void draw() throws sdljava.SDLException { // First, we clear the screen screen.fillRect(screen.mapRGB(0,0,0)); // Generate an iterator for the first item in the list ListIterator it = game_objects.listIterator(0); // Go through the list while( it.hasNext() ) { // Retrieve the next object in the list game_object current = (game_object) it.next(); // Draw it to the screen current.draw(screen); } screen.flip(); } 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 ); // Tell the game object class what size screen we're using game_object.set_screen_size(new c_vector(640.0f,480.0f)); // We want some means of storing a dynamic number of space ships. // We will need to peform various operations on them, such as updating and drawing them. // They will also need to be able to shoot at us, and we want to be able to shoot at them. // Thus, we also need a way of storing bullets, and a means of removing them from the game. // We will address both these points by considering both a ship and a bullet as a 'game object'. // If we then store these game objects in a list, we can cycle through the list to update and draw them. // If something gets created, it will be added to the list, and if it gets destroyed, it will be removed from the list. // // But why not put bullets in a list for each space ship? // If we did that, we would have a problem: what if the space ship gets blown up and is deleted? // The space ship's bullet list would also be deleted, and its active bullets removed (i.e. bullets that were fired by it, and are still flying through space). // We don't want this, so the bullets need to be stored outside the ship. // Create our space ship my_ship = new spaceship(SDLVideo.loadBMP("my_ship.bmp"), // The image to use for the ship new c_vector(320,240), // The starting position of the ship 300.0f, // The speed of the ship new c_vector(0,-1), // The direction that bullets are fired at SDLVideo.loadBMP("my_bullet.bmp"), // The image to use for the bullets 500.0f); // The speed of the bullets // Create our main game object list game_objects = new LinkedList<game_object>(); // Add the space ship to the list game_objects.add((game_object)my_ship); // We will use this for storing the direction that the user input indicates the ship should move in ship_direction = new c_vector(); // Timing is one of the most crucial aspects of an interactive game-like program. // Since the graphics on screen can take a variable amount of time to draw, // we cannot rely on a constant frame rate. Besides, if we run our program on a // faster or slower machine, it will be different again. // We must therefore calculate the amount of time that has elapsed between each // iteration of our game loop, and use this when we update things. long previous_time = 0, current_time = 0, deltaT = 0; float deltaT_s = 0.0f; current_time = System.currentTimeMillis(); previous_time = current_time; // 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(deltaT_s); // Finally, we need to draw our scene draw(); // Update our current time current_time = System.currentTimeMillis(); // Work out the frame rate for that frame deltaT_s = (float)(current_time-previous_time)/1000.0f; // For the next frame, our current time will the the previous one previous_time = current_time; } exit(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 encountered a problem"); exit(1); } } }