系列二:窗体出世
系列一完成后,总想跃跃欲试地看到怎么出现一个窗体
1.窗体的第一个版本
MainGame.h
1 #pragma once 2 #include <SDL/SDL.h> 3 #include <GL/glew.h> 4 5 class MainGame 6 { 7 public: 8 MainGame(); 9 ~MainGame(); 10 11 void run(); 12 void initSystems(); 13 14 private: 15 SDL_Window* m_pWindow; 16 int m_nScreenWidth; 17 int m_nScreenHeight; 18 };
MainGame.cpp
1 #include "MainGame.h" 2 3 4 MainGame::MainGame() :m_pWindow(nullptr), m_nScreenWidth(1024), m_nScreenHeight(768) 5 { 6 7 } 8 9 10 MainGame::~MainGame() 11 { 12 } 13 14 void MainGame::run() 15 { 16 initSystems(); 17 } 18 19 void MainGame::initSystems() 20 { 21 //Initialize SDL 22 SDL_Init(SDL_INIT_EVERYTHING); 23 m_pWindow = SDL_CreateWindow("GraphicToturial", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, m_nScreenWidth, m_nScreenHeight, SDL_WINDOW_OPENGL); 24 }
2.第一个窗体的版本仅仅是一个无法控制的版本,让咱们加点元素给他,让他能够控制
这里用到了SDL_PollEvent
当事件为SDL_QUIT:可以退出程序, 为SDL_MOUSEMOTION:可以捕捉鼠标移动的坐标
MainGame.h
1 #pragma once 2 #include <SDL/SDL.h> 3 #include <GL/glew.h> 4 5 enum class GameState{PLAY, EXIT}; 6 7 class MainGame 8 { 9 public: 10 MainGame(); 11 ~MainGame(); 12 13 void run(); 14 15 16 private: 17 void initSystems(); 18 void gameLoop(); 19 void processInput(); 20 21 SDL_Window* m_pWindow; 22 int m_nScreenWidth; 23 int m_nScreenHeight; 24 GameState m_gameState; 25 };
MainGame.cpp
1 #pragma once 2 #include <SDL/SDL.h> 3 #include <GL/glew.h> 4 5 enum class GameState{PLAY, EXIT}; 6 7 class MainGame 8 { 9 public: 10 MainGame(); 11 ~MainGame(); 12 13 void run(); 14 15 16 private: 17 void initSystems(); 18 void gameLoop(); 19 void processInput(); 20 21 SDL_Window* m_pWindow; 22 int m_nScreenWidth; 23 int m_nScreenHeight; 24 GameState m_gameState; 25 };