testman.cpp
author Mohammed Khoory
Wed Jul 07 19:11:38 2010 -0400 (22 months ago)
changeset 5 bcf3d6f89c62
permissions -rw-r--r--
Keyboard support, code movement, and TestManager

Major Changes
=============
- Added "testman.h" and "testman.cpp" to be used exclusively to test stuff (from a
GameManager perspective)
- Moved all the testing stuff to testman.cpp. 9game is just an empty class now to
manage other GameManagers
- Moved all the initializing stuff from 9Game to main. 9Game is like any other
GameManager now (just that it manages other GameManagers)
- Added keyboard.h and keyboard.c to handle keyboard interrupts (by replacing the
keyboard ISR)
- Keyboard is working :D can read more than one key at a time. (tested in
TestManager with WASD)

Minor Changes
=============
- Removed the exception throwing in SpriteLoader::unloadFile() (because it just
makes things unnecessarily complicated)
- added finished() to GameManager to indicate whether a GameManager is finished or
not
- Animation test + key control test. Sprite moves around depending on wasd key
input. (program ends when off screen to right)
- Set 0x00 as transparency color with drawSprite (TODO: change to make this
customizeable on runtime)
- Edited Makefile to reflect new changes

Notes
=====
- Animation seems to work nice and smooth in dosbox, slow in dosemu, so maybe I
should start testing in both dosemu and dosbox
- TODO: fix bug with drawSprite. When a sprite is drawn on the right of the screen
it "wraps around" to the other side by one pixel in width
     1 
     2 #include "testman.h"
     3 
     4 TestManager::TestManager()
     5 {
     6 	// load sprite
     7 	SpriteLoader spriteloader;
     8 	try{
     9 		spriteloader.loadFile("C:\\9game\\DJGPP\\sprites\\sprites");
    10 		
    11 		printf("debugging info from TestManager:\n");
    12 		printf("Width: %ld Height: %ld \n", spriteloader.getWidth(), spriteloader.getHeight());
    13 		printf("Size: %lu\n" , spriteloader.getSize());
    14 		
    15 		testSprite = spriteloader.getSprite(0,0,110,110);
    16 	}
    17 	catch(int e)
    18 	{
    19 		printf("an exception has occurred\n");
    20 		switch(e){
    21 		case SpriteLoader::FileNotLoadedException:
    22 			printf("Tried to run a function when no file loaded\n");
    23 			break;
    24 		case SpriteLoader::BadFileNameException:
    25 			printf("File name is not valid\n");
    26 			break;
    27 		case SpriteLoader::BadFileException:
    28 			printf("This file is bad. makes no sense\n");
    29 			break;
    30 		case SpriteLoader::CannotReadException:
    31 			printf("This file is not supported\n");
    32 			break;
    33 		}
    34 		if(spriteloader.isLoaded())
    35 			spriteloader.unloadFile();
    36 	}
    37 	
    38 	spriteX = 20;
    39 	spriteY = 20;
    40 	
    41 	wKey = false;
    42 	aKey = false;
    43 	sKey = false;
    44 	dKey = false;
    45 }
    46 
    47 TestManager::~TestManager()
    48 {
    49 	// free up memory from sprite
    50 	if(testSprite != NULL)
    51 	{
    52 		delete[] testSprite->bitmap;
    53 		delete testSprite;
    54 		testSprite = NULL;
    55 	}
    56 }
    57 
    58 int TestManager::processKeys()
    59 {
    60 	wKey = keyPressed(0x11) > 0;
    61 	aKey = keyPressed(0x1E) > 0;
    62 	sKey = keyPressed(0x1F) > 0;
    63 	dKey = keyPressed(0x20) > 0;
    64 	return 0;
    65 }
    66 
    67 int TestManager::updateState()
    68 {
    69 	if(wKey)
    70 		spriteY -= 1;
    71 	if(sKey)
    72 		spriteY += 1;
    73 	if(aKey)
    74 		spriteX -= 1;
    75 	if(dKey)
    76 		spriteX += 1;
    77 	return 0;
    78 }
    79 
    80 int TestManager::render(GraphicsDevice& graphics)
    81 {
    82 	// clear screen
    83 	graphics.clearScreen();
    84 	
    85 	// make all objects render themselves in order
    86 	
    87 	graphics.drawPixel(100, 100, 0x0F);
    88 	graphics.drawHorizontalLine(100, 110, 50, 0x0F);
    89 	graphics.drawVerticalLine(100, 120, 50, 0x0F);
    90 	if(testSprite != NULL && testSprite->bitmap != NULL)
    91 		graphics.drawSprite(spriteX,spriteY, testSprite);
    92 	else
    93 		graphics.drawHorizontalLine(5,10,25,0x0F);
    94 	
    95 	// display keyboard button presses
    96 	if(wKey)
    97 		graphics.drawPixel(50, 140, 0x0F);
    98 	if(aKey)
    99 		graphics.drawPixel(50, 150, 0x0F);
   100 	if(sKey)
   101 		graphics.drawPixel(50, 160, 0x0F);
   102 	if(dKey)
   103 		graphics.drawPixel(50, 170, 0x0F);
   104 	
   105 	
   106 	//update buffer
   107 	graphics.updateBuffer();
   108 
   109 	return 0;
   110 }
   111 
   112 int TestManager::registerGameObject(GameObject * aGameObject)
   113 {
   114 	return 0;
   115 }
   116 
   117 int TestManager::finished()
   118 {
   119 	if( spriteX > 320 )
   120 		return FINISHED;
   121 	
   122 	return NOT_FINISHED;
   123 }