Graphics, input & more!
Graphics Input Sound … game loop?
Tot nu toe alles in console
◦ Niet ideaal voor games, maar het kan wel!
Een venster maken
◦ Geen console, eentje waar je iets in kan tekenen.
Een game loop maken ◦ Net zoals in XNA
User input regelen ◦ Net zoals in XNA
Graphics
◦ In OpenGL – niet zoals in XNA
freeglut ◦ ◦ ◦ ◦
Window management Input Simple rendering (bijv tekst) Ter vervanging van GLUT (beetje gedateerd)
GLFW
◦ Window management ◦ Input ◦ Meer future proof
includes libs dlls #include een paar bestanden Link de libraries Zet de dll file in de juiste map
Alvast gecompileerd
Freeglut files in D:\lib\freeglut\ Creëer een empty project MyGame Project -> MyGame Properties…
Configuration: All Configurations Tab: VC++ Directories
Aangeven welke .lib files je wil gebruiken
freeglut.lib nodig
We maken main.cpp aan #include
int main(int argc, char* argv[]) { }
freeglut is C-style, niet object-oriented Enkele functies ◦ ◦ ◦ ◦ ◦ ◦ ◦
glutInit() glutInitDisplayMode() glutInitWindowSize() glutCreateWindow() glutFullScreen() glutMainLoop() Nog veel meer
Wat zijn callbacks? glutReshapeFunc() glutDisplayFunc() glutKeyboardFunc()
◦ glutKeyboardUpFunc()
glutMouseFunc()
◦ glutPassiveMotionFunc()
glutSpecialFunc()
◦ glutSpecialUpFunc()
en veel meer
#include void Resize(int width, int height); void Draw(); int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); glutInitWindowSize(800, 600); glutInitWindowPosition(0, 0); glutCreateWindow("My Game"); // Callbacks glutReshapeFunc(Resize); glutDisplayFunc(Draw);
}
glutMainLoop(); return 0;
void Draw() { glutPostRedisplay(); }
Waar is update?
void Update() { } void Draw() { Update(); }
glutPostRedisplay();
Hoe vaak wordt draw aangeroepen?
void Update(float dt) { } void Draw() { float currTime = glutGet(GLUT_ELAPSED_TIME) / 1000.f; float deltaTime = currTime - g_lastTime; g_lastTime = currTime; Update(deltaTime); }
glutPostRedisplay();
void Update(float dt) { } void Draw() { float currTime = glutGet(GLUT_ELAPSED_TIME) / 1000.f; float deltaTime = currTime - g_lastTime; if(deltaTime < 1/60.f) { Sleep(2); glutPostRedisplay(); return; } g_lastTime = currTime;
}
Update(deltaTime); glutPostRedisplay();
void KeyboardDown(unsigned char key, int x, int y); glutKeyboardFunc(KeyboardDown); void KeyboardDown(unsigned char key, int x, int y) { if(key == 'w') cout<<"W pressed"<<endl; if(key == 's') cout<<"S pressed"<<endl; }
void KeyboardSpecialDown(int key, int x, int y); glutSpecialFunc(KeyboardSpecialDown); void KeyboardSpecialDown(int key, int x, int y) { if(key == GLUT_KEY_LEFT) cout<<"Left pressed"<<endl; if(key == GLUT_KEY_RIGHT) cout<<"Right pressed"<<endl; }
Games willen liever geen input via callbacks
Liever state opvragen wanneer nodig.
In XNA
◦ Keyboard.GetState() ◦ KeyboardState.IsKeyDown()
Keyboard state bijhouden Enum van keys maken enum Keys { Left = GLUT_KEY_LEFT, Up = GLUT_KEY_UP, Right = GLUT_KEY_RIGHT, Down = GLUT_KEY_DOWN, Space = 32, LeftControl = GLUT_KEY_CTRL_L, Delete = 127, Esc = 27, };
class KeyboardState { public: KeyboardState() { memset(keyState, 0, 256); } bool bool bool bool
IsKeyDown(unsigned char keyCode) { return keyState[keyCode]; } IsKeyDown(Keys key) { return keyState[key]; } IsKeyUp(unsigned char keyCode) { return !keyState[keyCode]; } IsKeyUp(Keys key) { return !keyState[key]; }
static KeyboardState GetState() { return _kbState; } static void HandleKeyDown(unsigned char keyCode) { _kbState.keyState[keyCode] = true; } static void HandleKeyUp(unsigned char keyCode) { _kbState.keyState[keyCode] = false; } private: bool keyState[256]; } _kbState;
void KeyboardDown(unsigned char key, int x, int y) { KeyboardState::HandleKeyDown(key); } void KeyboardUp(unsigned char key, int x, int y) { KeyboardState::HandleKeyUp(key); } void Update(float dt) { KeyboardState kb = KeyboardState::GetState(); if(kb.IsKeyDown(Keys::Left)) cout<<"left key pressed"<<endl; }
Niet standaad in C++ Wel heel veel libraries ◦ ◦ ◦ ◦ ◦ ◦
OpenAL SDL FMOD Miles irrKlang En veel meer
Verschillende licenses, verschillende prijzen
Gratis voor niet-commercieel gebruik €65 euro voor indie license
Makkelijk in gebruik
Weer eens wat anders
Link irrklang.lib Kopieer de .dll #include using namespace irrklang; ISoundEngine* engine = createIrrKlangDevice(); if(engine == 0) return 0; engine->play2D("myfile.ogg"); engine->drop();