Commits

CSBerger committed 60977d3

GLFW3 basic functionality working

Comments (0)

Files changed (14)

GLGraphicsFont.cpp

 	FTFont* font = f.GetFont(font_name.c_str(), size);
 	float descender = font->Descender();
 //    ftglRenderFont(FTGLfont* font, const char *string, int mode);
-    cerr << render_string << "(" << render_string.length() << " " << x << ": " << y - descender << ")" << endl;
+    //cerr << render_string << "(" << render_string.length() << " " << x << ": " << y - descender << ")" << endl;
 //    glPushMatrix();
     glPushAttrib(GL_ALL_ATTRIB_BITS);
 
 	//Get Keyboard Input
 	for (auto i = actionVec.begin(); i != actionVec.end(); ++i) {
 		if(actionsToKeyBindings[*i] < JOY_MASK) {		
-			int d = glfwGetKey(actionsToKeyBindings[*i]);
+			int d = glfwGetKey(getWindow(), actionsToKeyBindings[*i]);
 			actionButtonsHeldDown[*i] = (d == 1);
 		}
 		else {
         joystick_num = *i;
         
         vector<pair<int, InputActions> > bound_axes_on_this_device = action_map[joystick_num];
-        float pos[6];
-        glfwGetJoystickPos(joystick_num, pos, 6);
+		const float* pos;
+		int* count = new int();
+		pos = glfwGetJoystickAxes(joystick_num, count);
+		if (*count > 0) {
         for (auto axis_it = bound_axes_on_this_device.begin(); axis_it != bound_axes_on_this_device.end(); ++axis_it) {
             steering_axes[axis_it->second] = pos[(*axis_it).first];
             if ((*axis_it).first < 0) {
                 steering_axes[axis_it->second] *= -1;
             }
         }
+		}
     }
     steeringAxes[0] = clamp(steering_axes[ANALOG_YAW], -1.0, 1.0);
     steeringAxes[1] = clamp(steering_axes[ANALOG_PITCH], -1.0, 1.0);

MainMenuScreen.cpp

 #include "MainMenuScreen.h"
 
 //#include "GL/glfw.h"
+#include "GLFW/glfw3.h"
+#include "InputHandler.h"
 const double render_period = 1.0/60.0;
 
 void MainMenuScreen::init(Menu* m, Renderer* r, int res_x, int res_y) {
 	renderer->renderMainMenu(*this);
 }
 
-void GLFWCALL kCallback(int key, int action) {
+void  kCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
 	if (action == GLFW_PRESS) {
 		switch (key) {
 		case GLFW_KEY_DOWN:
 	}
 }
 
-void GLFWCALL mCallback(int x, int y) {
+void mCallback(GLFWwindow* window, double x, double y) {
 	MainMenuScreen& instance = MainMenuScreen::Instance();
 	for (auto item = instance.getMenu().getItemsBegin(); item != instance.getMenu().getItemsEnd(); ++item) {
 		if (pointInRect(x, instance.getResolutionY() - y, item->getFrame())) {
 	}
 }
 
-int MainMenuScreen::run() {
+int MainMenuScreen::run(InputHandler& handler) {
 	timer = 0.0;
 	const double dt = 0.01;
 	double currentTime = glfwGetTime();
 	bool isRunning = true;
 	int result = -1;
 	double accumulator = dt;
-	glfwSetKeyCallback(kCallback);
-	glfwSetMousePosCallback(mCallback);
+	glfwSetKeyCallback(handler.getWindow(), kCallback);
+	glfwSetCursorPosCallback(handler.getWindow(), mCallback);
 	while (isRunning) {
 		double deltaTime = glfwGetTime();
 		accumulator += deltaTime - currentTime;
 			//LOGMSG(TAG, "render");
 
 		}
-
-		isRunning =  !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
+		//LOGMSG(TAG, "outside render");
+		isRunning =  !glfwGetKey(handler.getWindow(), GLFW_KEY_ESCAPE);
 
 
 	}
 #include "menu.h"
 #include "renderer.h"
 #include "Banner.h"
+#include "InputHandler.h"
 class MainMenuScreen
 {
 public:
 	void init(Menu* m, Renderer* r, int res_x, int res_y);
 	void keyCallback(int key, int action);
 	void render();
-	int run();
+	int run(InputHandler& handler);
 	Menu& getMenu() const {return *menu;};
 	void prev() {menu->prev(); LOGMSG(TAG,"%s", menu->getCurrent()->getLabelText());};
 	void next() {menu->next(); LOGMSG(TAG,"%s", menu->getCurrent()->getLabelText());};
 using namespace std;
 
 void SingleLogger::log(int type, string message) {
-	if (type != 6  &&  type != 4 ) {
+	if (type != 6  &&  type != 4 && type != 9) {
 		cerr << "(" << type << ") MSG:" << message << endl;
 	}
 }
 
 class Gamestate {
 public:
-/*	Gamestate() : objects(), ships(), bullets(),player(NULL), mission_name(), meshes(), playerController(new PlayerController(new InputHandler())), targetMap() {};
- */
+	Gamestate() : objects(), ships(), bullets(),player(NULL), mission_name(), meshes(), playerController(new PlayerController(InputHandler()		)	), targetMap() {};
 	Gamestate(PlayerController* p_controller) : objects(), ships(), bullets(),player(NULL), mission_name(), meshes(), playerController(p_controller), targetMap() {};
 	void init(map<string,string>& shipToMeshfile, string MissionFile);
 	EntityList & getObjects() {return objects;};
   </PropertyGroup>
   <ItemDefinitionGroup>
     <Link>
-      <AdditionalDependencies>freetype250_D.lib;ftgl_static_D.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;opengl32.lib;glu32.lib;wsock32.lib;comctl32.lib;glfw.lib;FreeImage.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>freetype250_D.lib;ftgl_static_D.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;opengl32.lib;glu32.lib;wsock32.lib;comctl32.lib;glfw3.lib;FreeImage.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <AdditionalLibraryDirectories>D:\Projects\Nightspace\external_libs;D:\Libs\freetype-2.5.0\objs\win32\vc2010;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
     </Link>
     <ClCompile>
       currentTime = newTime;
       s->step(frameTime*100);
 	  drawFrame(*renderer, gs);
-	  glfwSwapBuffers();
+	  glfwSwapBuffers(handler.getWindow());  //FIXME, window should be somewhere else
+	  glfwPollEvents();
 //	MainMenuScreen screen();	  
-	game_is_running =  !glfwGetKey(GLFW_KEY_ESC | GLFW_KEY_LCTRL) && glfwGetWindowParam(GLFW_OPENED);
+	game_is_running =  !glfwGetKey(handler.getWindow(), GLFW_KEY_ESCAPE | GLFW_KEY_LEFT_CONTROL); //FIXME, window should be somewhere else
   }
   return 0;
 }
 	Menu* menu = createMenu();
 	MainMenuScreen::Instance().init(menu, renderer,res_x, res_y );	
 }
-int menu_loop(Renderer* renderer) {
+int menu_loop(Renderer* renderer, InputHandler& handler) {
 	const double dt = 0.01;
 	double currentTime = glfwGetTime();
 	double accumulator = dt;
 	bool onMenu = true;
 	Menu* m = createMenu();
 
-	MainMenuScreen::Instance().run();
+	MainMenuScreen::Instance().run(handler);
 
 	return 0;
 }
-EntityList initEntityList() {
+EntityList initEntityList(InputHandler& handler) {
     EntityList eList;
     Mesh * wFighter = new Mesh();
     wFighter->loadMeshFromFile("./art/xwing.bas");
 	wFighter->loadMeshFromFile("./art/xwing.ply" );
     Controller* dumb = new DummyController();
-    Controller* player = new PlayerController();
+    Controller* player = new PlayerController(handler);
     Ship* playerShip = new Ship("Chris", *wFighter, CVec3(0,0,0), CVec3(0,0,0), CVec3(1,0,0), 0.0f, player);
     eList.push_back(playerShip);
     return eList;
 bool fullscreen = true;
 #endif
 	glfwInit();
-    unsigned int fullscreen_flag = fullscreen ? GLFW_FULLSCREEN: GLFW_WINDOW;
-    
+//    unsigned int fullscreen_flag = fullscreen ? GLFW_FULLSCREEN: GLFW_WINDOW;
+	GLFWmonitor* monitor = glfwGetPrimaryMonitor();
+/*
 	if (fullscreen) {
 		glfwOpenWindowHint(GLFW_FULLSCREEN, GL_TRUE);
 	}
 	else {
 		glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);
 	}
-    GLFWwindow * window_handle = glfwCreateWindow();
+*/
+	GLFWwindow * window_handle = glfwCreateWindow(res_x, res_y, "Nightspace", NULL, NULL);
+	glfwMakeContextCurrent(window_handle);
+
+/*
 	if (!glfwOpenWindow(res_x, res_y, 8, 8, 8, 8, 8, 8, fullscreen_flag)) {
         glfwTerminate();
     }
-	glfwSetWindowPos(350, 150);
+*/
+//	glfwSetWindowPos(350, 150);
     bool running = true;
-
+    InputHandler handler;
     EntityList e;
-    e = initEntityList();
+    e = initEntityList(handler);
     clock_t t, dt, fps_counter_start;
     int test;
     Gamestate gs;
     gs.init(shipToMeshfile, "./mission1.txt");
     Camera* camera = new ChaseCamera(gs);
     bool lightingOn = true;
-    InputHandler handler;
+
 
     handler.setWindow(window_handle);
 	handler.loadKeyBindingsFromDefault();
     enum InputType primary_controller = handler.getPrimaryGameController();
     LOGMSG(TAG, "%d", primary_controller);
     Simulator* s = new Simulator(gs, handler);
-    OpenGlRenderer* rend = new OpenGlRenderer(camera, s->getPerception(), res_x, res_y);
+    OpenGlRenderer* rend = new OpenGlRenderer(window_handle, camera, s->getPerception(), res_x, res_y);
     map<int, int> bindings;
 
 	init_main_menuscreen(rend, res_x, res_y);
-	int menu_result = menu_loop(rend);
+	int menu_result = menu_loop(rend, handler);
 //	runMenu();
     int run_result = run_loop(s, rend, gs, handler);
     

nightspace.vcxproj

     <ClInclude Include="gui.h" />
     <ClInclude Include="gunshot.h" />
     <ClInclude Include="HudElement.h" />
+    <ClInclude Include="inc\FTGL\FTBBox.h" />
+    <ClInclude Include="inc\FTGL\FTBitmapGlyph.h" />
+    <ClInclude Include="inc\FTGL\FTBuffer.h" />
+    <ClInclude Include="inc\FTGL\FTBufferFont.h" />
+    <ClInclude Include="inc\FTGL\FTBufferGlyph.h" />
+    <ClInclude Include="inc\FTGL\FTExtrdGlyph.h" />
+    <ClInclude Include="inc\FTGL\FTFont.h" />
+    <ClInclude Include="inc\FTGL\ftgl.h" />
+    <ClInclude Include="inc\FTGL\FTGLBitmapFont.h" />
+    <ClInclude Include="inc\FTGL\FTGLExtrdFont.h" />
+    <ClInclude Include="inc\FTGL\FTGLOutlineFont.h" />
+    <ClInclude Include="inc\FTGL\FTGLPixmapFont.h" />
+    <ClInclude Include="inc\FTGL\FTGLPolygonFont.h" />
+    <ClInclude Include="inc\FTGL\FTGLTextureFont.h" />
+    <ClInclude Include="inc\FTGL\FTGlyph.h" />
+    <ClInclude Include="inc\FTGL\FTLayout.h" />
+    <ClInclude Include="inc\FTGL\FTOutlineGlyph.h" />
+    <ClInclude Include="inc\FTGL\FTPixmapGlyph.h" />
+    <ClInclude Include="inc\FTGL\FTPoint.h" />
+    <ClInclude Include="inc\FTGL\FTPolyGlyph.h" />
+    <ClInclude Include="inc\FTGL\FTSimpleLayout.h" />
+    <ClInclude Include="inc\FTGL\FTTextureGlyph.h" />
     <ClInclude Include="InputHandler.h" />
     <ClInclude Include="MainMenuScreen.h" />
     <ClInclude Include="menu.h" />

nightspace.vcxproj.filters

     <Filter Include="Controllers">
       <UniqueIdentifier>{f5239348-dfbb-43fd-81fd-ec84e793312a}</UniqueIdentifier>
     </Filter>
+    <Filter Include="Header Files\inc">
+      <UniqueIdentifier>{f579de8d-bc23-41a5-b772-6778bf6bf888}</UniqueIdentifier>
+    </Filter>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="camera.cpp">
     <ClInclude Include="SingleLogger.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="inc\FTGL\FTBBox.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTBitmapGlyph.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTBuffer.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTBufferFont.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTBufferGlyph.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTExtrdGlyph.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTFont.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\ftgl.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTGLBitmapFont.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTGLExtrdFont.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTGLOutlineFont.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTGLPixmapFont.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTGLPolygonFont.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTGLTextureFont.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTGlyph.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTLayout.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTOutlineGlyph.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTPixmapGlyph.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTPoint.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTPolyGlyph.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTSimpleLayout.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
+    <ClInclude Include="inc\FTGL\FTTextureGlyph.h">
+      <Filter>Header Files\inc</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="nightspace.rc">

openGlRenderer.cpp

 {"./art/slice_posz.bmp", "./art/slice_minz.bmp", "./art/slice_posx.bmp", "./art/slice_minx.bmp", "./art/slice_miny.bmp", "./art/slice_posy.bmp"};
 GLfloat light_pos[] = {0.0, 0.0, -10.0, 1.0};
 
-OpenGlRenderer::OpenGlRenderer(Camera* cam, Perception* per, int width, int height) : Renderer(per), camera(cam), init(false), shader_manager(0), current_program(0) , guiEnabled(true), res_width(width), res_height(height){
+OpenGlRenderer::OpenGlRenderer(GLFWwindow* wind, Camera* cam, Perception* per, int width, int height) : wind(wind), Renderer(per), camera(cam), init(false), shader_manager(0), current_program(0) , guiEnabled(true), res_width(width), res_height(height){
     //  setupLight();
     skyTextures = (GLuint*)calloc(6, sizeof(GLuint));
     menuInit = false;
     glDisable(GL_TEXTURE_2D);
     glEnable(GL_DEPTH_TEST);
     glEnable(GL_LIGHTING);
-    glfwSwapBuffers(null);
+    glfwSwapBuffers(wind);
+	glfwPollEvents();
 }
 
 void OpenGlRenderer::init_graphics() {
     Menu& m = mm.getMenu();
 
     glDisable(GL_DEPTH_TEST);
+	glewExperimental = GL_TRUE;
     GLenum err = glewInit();
 if (GLEW_OK != err)
 {
 
 class OpenGlRenderer : public Renderer {
 public:
-  OpenGlRenderer(Camera*, Perception*, int width, int height);
+  OpenGlRenderer(GLFWwindow* wind, Camera*, Perception*, int width, int height);
 	virtual ~OpenGlRenderer() {};
 	virtual void renderGame(Gamestate& gs);
 	virtual void renderGui(Gamestate& gs);
     GLfloat* initRectVertexArrayMemory(const std::vector<Rect>&) const;
     void testVertexArrayCreate() const;
     void runInitTests();
+	GLFWwindow* wind;
+
 	int res_width;
 	int res_height;
 	std::vector<HudElement*> hud_element_list;

playerController.cpp

     // *  Handle Pitch
     // */
 
-    if (glfwGetKey(handler.getWindow(), GLFW_KEY_UP)) {
+    if ((*keys)[DOWN]) {
         dPitch = -1;
     }
-    if (glfwGetKey(handler.getWindow(), GLFW_KEY_DOWN)) {
+    if ((*keys)[UP]) {
         dPitch = 1;
     }
     dPitch = abs(pitch) > abs(dPitch)? pitch: dPitch;
         dRoll = -1;
     }
 
-    if (glfwGetKey('.')) {
+    if ((*keys)[ROLL_LEFT]) {
         dRoll = 1;
     }
     dRoll = abs(roll) > abs(dRoll)? roll: dRoll;
 
 void Simulator::step(float dTimeSeconds) {
 	inputHandler.translateInputToActions(action_buttons, steering_direction);
+	stringstream s;
+	s << "steering direction " << steering_direction[0]		<< "" << endl;
+	LOGMSG(7, s.str().c_str());	
 	gs.getPlayerController()->load_input(action_buttons, steering_direction);
 	decideActions();
 	processActions(dTimeSeconds);