Hi,
I was just experiencing some things in a 2D game to use in another game... In this experience you have ship that should fire whenever you press SPACE... but it doesn''t... look at the code:
Player.cpp
#include "Player.h"
CPlayer::CPlayer()
{
health = 100.0f;
xPos = 0.0f;
yPos = -6.5;
}
CPlayer::~CPlayer()
{}
void CPlayer::Draw()
{
glPushMatrix();
glTranslatef(xPos, yPos, -18.0);
glBegin(GL_QUADS);
glVertex2f(-0.2, -0.2);
glVertex2f(0.2, -0.2);
glVertex2f(0.2, 0.2);
glVertex2f(-0.2, 0.2);
glEnd();
glPopMatrix();
}
void CPlayer::ChangeDirection(int direction)
{
if(direction == 1) //Right
{
xPos += 0.01f;
}
if(direction == 2) //Left
{
xPos -= 0.01f;
}
}
void CPlayer::Fire()
{
CRocket *Rocket = new CRocket;
Rocket->xPos = xPos;
Rocket->yPos = yPos;
Rocket->Draw();
}
Rocket.cpp
#include "Rocket.h"
CRocket::CRocket()
{
xPos = 0.0;
yPos = -6.5;
}
CRocket::~CRocket()
{}
void CRocket::Draw()
{
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glTranslatef(xPos, yPos, -18.0);
glBegin(GL_QUADS);
glVertex2f(-0.04, -0.04);
glVertex2f(0.04, -0.04);
glVertex2f(0.04, 0.04);
glVertex2f(-0.04, 0.04);
glEnd();
glColor3f(1.0, 1.0, 1.0);
glPopMatrix();
yPos += 0.01f;
}
MainWnd.cpp
#include "MainWnd.h"
CDirectInput DirectInput;
CPlayer *Player;
CRocket *Rocket;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool CApp::Initialize()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
if(!DirectInput.Initialize(glWindow.window.hWnd, glWindow.application.hInstance, true, IS_USEKEYBOARD | IS_USEMOUSE)) return false;
Player = new CPlayer;
Rocket = new CRocket;
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CApp::GameCycle()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_SMOOTH);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
Player->Draw();
glFlush();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CApp::Deinitialize()
{
DirectInput.Unload();
delete Player;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CApp::GetInput()
{
DirectInput.Update();
if (!inputInUse)
{
// quit
if (DirectInput.KeyDown(DIK_ESCAPE))
glWindow.TerminateApplication(&glWindow.window);
if(DirectInput.KeyDown(DIK_RIGHT)) Player->ChangeDirection(1);
if(DirectInput.KeyDown(DIK_LEFT)) Player->ChangeDirection(2);
if(DirectInput.KeyDown(DIK_SPACE)) Player->Fire();
if (DirectInput.KeyDown(DIK_L)) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
if (DirectInput.KeyDown(DIK_F)) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
if (DirectInput.KeyDown(DIK_P)) glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
}
}
See the problem?
Thanks a lot
"Through me the road to the city of desolation,Through me the road to sorrows diuturnal,Through me the road among the lost creation."