mirror of
				https://github.com/MichaelFisher1997/opengl-cpp.git
				synced 2025-10-18 12:17:45 +00:00 
			
		
		
		
	Added GLEW
This commit is contained in:
		
							
								
								
									
										37
									
								
								src/sdl.cpp
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								src/sdl.cpp
									
									
									
									
									
								
							| @@ -1,3 +1,4 @@ | |||||||
|  | #include <GL/glew.h> // Include GLEW before <SDL2/SDL.h>? | ||||||
| #include "sdl.hpp" | #include "sdl.hpp" | ||||||
| #include <iostream> | #include <iostream> | ||||||
|  |  | ||||||
| @@ -8,7 +9,9 @@ SdlWindow::SdlWindow(const char* title, int width, int height) | |||||||
|     m_isFullscreen(false), |     m_isFullscreen(false), | ||||||
|     m_width(width), |     m_width(width), | ||||||
|     m_height(height), |     m_height(height), | ||||||
|     m_glContext(nullptr) |     m_glContext(nullptr), | ||||||
|  |     m_windowedWidth(width), | ||||||
|  |     m_windowedHeight(height) | ||||||
| { | { | ||||||
|   // 1. Set attributes |   // 1. Set attributes | ||||||
|     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); |     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); | ||||||
| @@ -37,10 +40,12 @@ SdlWindow::SdlWindow(const char* title, int width, int height) | |||||||
|   // 4. Optionally init GLEW (if not on macOS core profile) |   // 4. Optionally init GLEW (if not on macOS core profile) | ||||||
|   #ifndef __APPLE__ |   #ifndef __APPLE__ | ||||||
|   glewExperimental = GL_TRUE; |   glewExperimental = GL_TRUE; | ||||||
|  |   GLenum glewErr = glewInit(); | ||||||
|   if (glewInit() != GLEW_OK) { |   if (glewInit() != GLEW_OK) { | ||||||
|       std::cerr << "Failed to init GLEW" << std::endl; |       std::cerr << "Failed to init GLEW" << glewGetErrorString(glewErr) << std::endl; | ||||||
|       return; |       return; | ||||||
|   } |   } | ||||||
|  |   glGetError(); | ||||||
|   #endif |   #endif | ||||||
|  |  | ||||||
|   // 5. Set vsync (optional) |   // 5. Set vsync (optional) | ||||||
| @@ -48,7 +53,6 @@ SdlWindow::SdlWindow(const char* title, int width, int height) | |||||||
|  |  | ||||||
|   // 6. Mark as running |   // 6. Mark as running | ||||||
|   m_isRunning = true; |   m_isRunning = true; | ||||||
|   m_isRunning = true; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| SdlWindow::~SdlWindow() { | SdlWindow::~SdlWindow() { | ||||||
| @@ -111,6 +115,11 @@ SDL_Event event; | |||||||
|         // Update your internal width/height (if you keep track) |         // Update your internal width/height (if you keep track) | ||||||
|         m_width  = newWidth; |         m_width  = newWidth; | ||||||
|         m_height = newHeight; |         m_height = newHeight; | ||||||
|  |         // If we are in windowed mode, update the "windowed" size. | ||||||
|  |         if (!m_isFullscreen) { | ||||||
|  |             m_windowedWidth  = newWidth; | ||||||
|  |             m_windowedHeight = newHeight; | ||||||
|  |         } | ||||||
|  |  | ||||||
|         // Update the OpenGL viewport |         // Update the OpenGL viewport | ||||||
|         glViewport(0, 0, newWidth, newHeight); |         glViewport(0, 0, newWidth, newHeight); | ||||||
| @@ -137,22 +146,26 @@ void SdlWindow::render() { | |||||||
|   SDL_GL_SwapWindow(m_window); |   SDL_GL_SwapWindow(m_window); | ||||||
| } | } | ||||||
|  |  | ||||||
| void SdlWindow::change_res(int newWidth, int newHeight) |  | ||||||
| { |  | ||||||
|   // Just call SDL_SetWindowSize |  | ||||||
|   if (m_window) { |  | ||||||
|     SDL_SetWindowSize(m_window, newWidth, newHeight); |  | ||||||
|   } |  | ||||||
| } |  | ||||||
|  |  | ||||||
| void SdlWindow::setFullscreen(bool fullscreen) { | void SdlWindow::setFullscreen(bool fullscreen) { | ||||||
|     if (m_window) { |     if (m_window) { | ||||||
|         m_isFullscreen = fullscreen; |         m_isFullscreen = fullscreen; | ||||||
|         if (m_isFullscreen) { |         if (m_isFullscreen) { | ||||||
|  |             // Going TO fullscreen: | ||||||
|  |             // (Optional) store the current size again, in case it changed | ||||||
|  |             m_windowedWidth  = m_width; | ||||||
|  |             m_windowedHeight = m_height; | ||||||
|  |  | ||||||
|             SDL_SetWindowFullscreen(m_window, SDL_WINDOW_FULLSCREEN_DESKTOP); |             SDL_SetWindowFullscreen(m_window, SDL_WINDOW_FULLSCREEN_DESKTOP); | ||||||
|         } else { |         } else { | ||||||
|  |             // Returning FROM fullscreen: | ||||||
|             SDL_SetWindowFullscreen(m_window, 0); // return to windowed |             SDL_SetWindowFullscreen(m_window, 0); // return to windowed | ||||||
|             change_res(m_width, m_height); |             // Now restore the window’s old size | ||||||
|  |             SDL_SetWindowSize(m_window, m_windowedWidth, m_windowedHeight); | ||||||
|  |  | ||||||
|  |             // Update m_width, m_height so they reflect the new (restored) size | ||||||
|  |             m_width  = m_windowedWidth; | ||||||
|  |             m_height = m_windowedHeight; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -24,7 +24,6 @@ public: | |||||||
|     // Run the main loop |     // Run the main loop | ||||||
|     void run(); |     void run(); | ||||||
|  |  | ||||||
|     void change_res(int newWidth, int newHeight); |  | ||||||
|     void setFullscreen(bool fullscreen); |     void setFullscreen(bool fullscreen); | ||||||
|  |  | ||||||
| private: | private: | ||||||
| @@ -35,7 +34,10 @@ private: | |||||||
|     bool          m_isFullscreen;  |     bool          m_isFullscreen;  | ||||||
|     int           m_width; |     int           m_width; | ||||||
|     int           m_height; |     int           m_height; | ||||||
|  |     int           m_windowedWidth;   // stored width before fullscreen | ||||||
|  |     int           m_windowedHeight;  // stored height before fullscreen | ||||||
|     SDL_GLContext  m_glContext; |     SDL_GLContext  m_glContext; | ||||||
|  |      | ||||||
|  |  | ||||||
|     // Private methods |     // Private methods | ||||||
|     void processEvents(); |     void processEvents(); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 MichaelFisher1997
					MichaelFisher1997