The source code of the graphics output for Linux/KDE. This code was written and tested with KDevelop.
/***************************************************************************
spaceinv.cpp - description
-------------------
begin : Sat Jun 23 02:30:43 CEST 2001
copyright : (C) 2001-2002 by Alessandro Scotti
email : dev@ascotti.org
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <stdio.h>
#include "spaceinv.h"
#include <qpainter.h>
SpaceInv::SpaceInv(QWidget *parent, const char *name) : QWidget(parent, name)
{
setMinimumSize( InvadersMachine::ScreenWidth, InvadersMachine::ScreenHeight );
setMaximumSize( InvadersMachine::ScreenWidth, InvadersMachine::ScreenHeight );
screen_ = new QImage( InvadersMachine::ScreenWidth, InvadersMachine::ScreenHeight, 8, 2 );
screen_->setColor( 0, qRgb(0x00,0x00,0x00) );
screen_->setColor( 1, qRgb(0xFF,0xFF,0xFF) );
arcade_ = new InvadersMachine();
unsigned char buffer[ 0x2000 ];
FILE * f = fopen( "invaders.rom", "rb" );
if( f != NULL ) {
if( fread( buffer, 0x2000, 1, f ) == 1 ) {
arcade_->setROM( buffer );
}
fclose( f );
}
startTimer( 16 );
}
SpaceInv::~SpaceInv()
{
delete screen_;
delete arcade_;
}
/*
Main game loop
*/
void SpaceInv::timerEvent( QTimerEvent * ev )
{
arcade_->step();
// Clear background
screen_->fill( 0 );
// Draw video
const unsigned char * video = arcade_->getVideo();
for( int y=0; y<InvadersMachine::ScreenHeight; y++ ) {
int c = 1;
for( int x=0; x<InvadersMachine::ScreenWidth; x++ ) {
if( *video++ ) screen_->setPixel( x, y, c );
}
}
QPainter painter( this );
painter.drawImage( 0, 0, *screen_ );
}
void SpaceInv::keyPressEvent( QKeyEvent * ev )
{
switch( ev->key() ) {
case Key_1:
arcade_->fireEvent( InvadersMachine::KeyOnePlayerDown );
break;
case Key_2:
arcade_->fireEvent( InvadersMachine::KeyTwoPlayersDown );
break;
case Key_3:
arcade_->fireEvent( InvadersMachine::CoinInserted );
break;
case Key_Left:
arcade_->fireEvent( InvadersMachine::KeyLeftDown );
break;
case Key_Right:
arcade_->fireEvent( InvadersMachine::KeyRightDown );
break;
case Key_Control:
case Key_Space:
arcade_->fireEvent( InvadersMachine::KeyFireDown );
break;
case Key_Escape:
break;
}
}
void SpaceInv::keyReleaseEvent( QKeyEvent * ev )
{
switch( ev->key() ) {
case Key_1:
arcade_->fireEvent( InvadersMachine::KeyOnePlayerUp );
break;
case Key_2:
arcade_->fireEvent( InvadersMachine::KeyTwoPlayersUp );
break;
case Key_Left:
arcade_->fireEvent( InvadersMachine::KeyLeftUp );
break;
case Key_Right:
arcade_->fireEvent( InvadersMachine::KeyRightUp );
break;
case Key_Control:
case Key_Space:
arcade_->fireEvent( InvadersMachine::KeyFireUp );
break;
}
}
/***************************************************************************
spaceinv.h - description
-------------------
begin : Sat Jun 23 02:30:43 CEST 2001
copyright : (C) 2001-2003 by Alessandro Scotti
email : dev@ascotti.org
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef SPACEINV_H
#define SPACEINV_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <kapp.h>
#include <qwidget.h>
#include <qimage.h>
#include "arcade.h"
/** SpaceInv is the base class of the porject */
class SpaceInv : public QWidget
{
Q_OBJECT
public:
/** construtor */
SpaceInv(QWidget* parent=0, const char *name=0);
/** destructor */
~SpaceInv();
protected:
void timerEvent( QTimerEvent * ev );
void keyPressEvent( QKeyEvent * ev );
void keyReleaseEvent( QKeyEvent * ev );
InvadersMachine * arcade_;
QImage * screen_;
};
#endif