This is probably the millionenth time ive sat down to learn c++, over the course of 6 years,
ive continuously made improvements
And over the last 6 months, i truely learnt some of the more advance areas of game programming, the theory atleast and pseudo code
The Thing no tutorial covers is the basic layout of a Game, and why it is that way for example
In lua i would store the player attributes in an array
and simply add or subtract from the appropriate values, in he main loop once a button was pressed
however in C++ it is totally different, or atleast the sample code is for example
a class would be created
class Player
{
int x,y,z;
public:
void SetLoc(int a,int b,int c){
x=a;
y=b;
z=c;
}
void Move(char a){
if (a=='F') z--;
else if (a=='B') z++;
else if (a=='L') x--;
else if (a=='R') x++;
else if (a=='U') y++;
else if (a=='D') y--;
}
};
now the sample code normally provides something like SetLoc, but why not stick x,y,x as public and simply put this before the main loop
Player().x=1.f
how should i handle movement at the moment, ive got an event listener which listens for a key in sdl,
in my main loop i have this
if (Ctrl::Up) Player().Move('F');
if (Ctrl::Down) Player().Move('B');
if (Ctrl::Left) Player().Move('L');
if (Ctrl::Right) Player().Move('R');
But why not just have Player().x++
Thanks
Also some tips on scope would be helpful
heres my full code
Theres redundant pieces here and there
#include <windows.h>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
float ToRad = PI/180;
float ToDeg = 180/PI;
namespace Ctrl
{
bool Up=false;
bool Down=false;
bool Left=false;
bool Right=false;
bool A=false;
bool S=false;
bool Space=false;
bool OldSpace=false;
}
// Handle events
bool Events()
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
{
if (event.key.keysym.sym == SDLK_ESCAPE) return false;
if (event.key.keysym.sym == SDLK_UP) Ctrl::Up=true;
else if (event.key.keysym.sym == SDLK_DOWN) Ctrl::Down=true;
if (event.key.keysym.sym == SDLK_LEFT) Ctrl::Left=true;
else if (event.key.keysym.sym == SDLK_RIGHT) Ctrl::Right=true;
if (event.key.keysym.sym == SDLK_a) Ctrl::A=true;
else if (event.key.keysym.sym == SDLK_s) Ctrl::S=true;
if (event.key.keysym.sym == SDLK_SPACE) Ctrl::Space=true;
break;
}
case SDL_KEYUP:
{
if (event.key.keysym.sym == SDLK_UP) Ctrl::Up=false;
else if (event.key.keysym.sym == SDLK_DOWN) Ctrl::Down=false;
if (event.key.keysym.sym == SDLK_LEFT) Ctrl::Left=false;
else if (event.key.keysym.sym == SDLK_RIGHT) Ctrl::Right=false;
if (event.key.keysym.sym == SDLK_a) Ctrl::A=false;
else if (event.key.keysym.sym == SDLK_s) Ctrl::S=false;
if (event.key.keysym.sym == SDLK_SPACE) Ctrl::Space=false;
break;
}
case SDL_QUIT:
return false;
}
}
return true;
}
class Player
{
public:
int x,y,z;
void SetLoc(int a,int b,int c){
x=a;
y=b;
z=c;
}
int RetLoc(){
return x,y,z;
}
void Move(char a){
if (a=='F') z--;
else if (a=='B') z++;
else if (a=='L') x--;
else if (a=='R') x++;
else if (a=='U') y++;
else if (a=='D') y--;
}
};
void InitGraphics(){
const int width = 800, height = 480;
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) exit(1);
// Window Settings
SDL_WM_SetCaption("SDL/OpenGL Sample", "SDL/OpenGL Sample");
// Get Video Info
const SDL_VideoInfo *video = SDL_GetVideoInfo();
if (!video) { SDL_Quit(); exit(2); }
// Create Video Surface
SDL_Surface *screen = SDL_SetVideoMode(width, height, video->vfmt->BitsPerPixel, SDL_OPENGL);
if (!screen) { SDL_Quit(); exit(3); }
// Size OpenGL to Video Surface
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)width / (float)height, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
// Set Pixel Format
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// OpenGL Render Settings
glClearColor(0, 0, 0, 1);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
}
void Draw(){
// Set location in front of camera
glLoadIdentity();
glTranslated(Player().x,Player().y,Player().z);
glRotated(0,0,0,1);
// Draw a outline polygon
glBegin(GL_LINE_LOOP);
glColor3d(1, 0, 0);
glVertex3d(0, 20, 0);
glVertex3d(20, -20, 0);
glVertex3d(0, 0, 0);
glVertex3d(-20, -20, 0);
glEnd();
}
// Program entry point
//int main(int argc, char **argv)
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
InitGraphics();
Player().SetLoc(-1.f,-1.f,-500.f);
while (Events())
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (Ctrl::Up) Player().Move('F');
if (Ctrl::Down) Player().Move('B');
if (Ctrl::Left) Player().Move('L');
if (Ctrl::Right) Player().Move('R');
Draw();
// Show the frame
SDL_GL_SwapBuffers();
}
// Clean up
SDL_Quit();
return 0;
}
It doesnt seem to be drawing anything because Draw is not playing ball, if i empty the code out of the function Draw and just paste it in, it works fine