本文主要是介绍第15章 《乐趣》Page305~311犯的错误,窗口GetID()为0的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Page305~311, 第一遍
//main.cpp
#include <iostream>
#include <SDL2/SDL.h>
#include "sdl_initiator.hpp"
#include "sdl_error.hpp"
#include "sdl_window.hpp"using namespace std;int main(int argc, char* argv[])
{sdl2::Initiator::Instance().Init(SDL_INIT_VIDEO| SDL_INIT_AUDIO| SDL_INIT_EVENTS| SDL_INIT_TIMER);if(!sdl2::Initiator::Instance())//重载转换符{cerr << "初始化就出错,没得玩了!"<< sdl2::last_error() << endl;return -1;}//创建并居中显示宽640高480的游戏窗口sdl2::Window wnd("hello sdl", sdl2::WindowPosition().Centered(true, true), 640, 480//使用空的特性标志, sdl2::WindowFlags());if(!wnd){cerr << sdl2::last_error() << endl;return -1;}//输出窗口IDcout << wnd.GetID() << endl;wnd.SetOpacity(0.5); //设置不透明度0.5,也就是相当于一半的透明度SDL_Delay(3000);wnd.Hide();SDL_Delay(1000);wnd.Show();SDL_Delay(1000);return 0;
}
//sdl_window.hpp
#ifndef SDL_WINDOW_HPP_INCLUDED
#define SDL_WINDOW_HPP_INCLUDED
#include <SDL2/SDL.h>namespace sdl2
{struct WindowPosition
{WindowPosition() //默认构造,_x,_y都使用系统默认位置: _x(SDL_WINDOWPOS_UNDEFINED), _y(SDL_WINDOWPOS_UNDEFINED)//SDL_WINDOWPOS_UNDEFINED会将窗口放到中间{}//设置为居中,注意返回当前对象的引用WindowPosition& Centered(bool x_centered = true, bool y_centered = true){if(x_centered)_x = SDL_WINDOWPOS_CENTERED;if(y_centered)_y = SDL_WINDOWPOS_CENTERED;}int _x, _y;
};struct WindowFlags
{WindowFlags() //默认构造,用于构建一个没有指定任何特性的普通窗口: _flags(0){}Uint32 _flags;
};struct Window
{
public:Window(char const* title, WindowPosition const& win_position, int w, int h, WindowFlags const& win_flags){_window = SDL_CreateWindow(title, win_position._x, win_position._y, w, h, win_flags._flags);}~Window(){if(_window) //_window不是空指针{SDL_DestroyWindow(_window);}}explicit operator bool() const{
// return 0 != _window;return _window != nullptr;}Uint32 GetID(){return SDL_GetWindowID(_window);}bool SetOpacity(float opacity){return 0 == SDL_SetWindowOpacity(_window, opacity);}void Hide(){SDL_HideWindow(_window);}void Show(){SDL_ShowWindow(_window);}SDL_Window* _window;
};}//sdl2#endif // SDL_WINDOW_HPP_INCLUDED
//sdl_initiator.hpp
#ifndef SDL_INITIATOR_HPP_INCLUDED
#define SDL_INITIATOR_HPP_INCLUDEDnamespace sdl2
{struct Initiator
{
private:Initiator()//默认构造: _init_result(-1){}
public:static Initiator& Instance(){static Initiator Instance;return Instance;}~Initiator(){if(_init_result == 0){SDL_Quit();}}void GetVersion(Uint8& major, Uint8& minor, Uint8& patch){SDL_version ver;SDL_GetVersion(&ver);major = ver.major;minor = ver.minor;patch = ver.patch;}bool Init(Uint32 flags = SDL_INIT_EVERYTHING){_init_result = SDL_Init(flags);return 0 == _init_result;}// bool operator bool()explicit operator bool() const{return _init_result == 0;}private:int _init_result;
};//Initiator}//sdl2#endif // SDL_INITIATOR_HPP_INCLUDED
//sdl_error.hpp
#ifndef SDL_ERROR_HPP_INCLUDED
#define SDL_ERROR_HPP_INCLUDED
#include <SDL2/SDL.h>namespace sdl2
{
char const* last_error();
}#endif // SDL_ERROR_HPP_INCLUDED
//sdl_error.cpp
#include "sdl_error.hpp"namespace sdl2
{char const* last_error()
{return SDL_GetError();
}}//sdl2
第二遍犯得的错误,见代码注释,第56行
//sdl_window.hpp
#ifndef SDL_WINDOW_HPP_INCLUDED
#define SDL_WINDOW_HPP_INCLUDED#include <SDL2/SDL.h>namespace sdl2
{struct WindowPosition
{WindowPosition()//默认构造: _x(SDL_WINDOWPOS_UNDEFINED), _y(SDL_WINDOWPOS_UNDEFINED){}WindowPosition(int x, int y)//常规构造: _x(x), _y(y){}WindowPosition& Centered(bool x_centered = true , bool y_centered = true){if(x_centered){_x = SDL_WINDOWPOS_CENTERED;}if(y_centered){_y = SDL_WINDOWPOS_CENTERED;}}int _x, _y;
};struct WindowFlags
{WindowFlags(): _flags(0){}Uint32 _flags;
};struct Window
{Window(char const* title, WindowPosition const& win_position, int w, int h, WindowFlags const& win_flags){
// SDL_CreateWindow(title //没有为_window赋值,将导致后面获取ID,等一系列的操作失效_window = SDL_CreateWindow(title, win_position._x, win_position._y, w, h, win_flags._flags);}~Window(){if(_window){SDL_DestroyWindow(_window);}}explicit operator bool () const{return _window != nullptr;}Uint32 GetID(){SDL_assert(_window != nullptr);return SDL_GetWindowID(_window);}void SetOpacity(float opacity){SDL_assert(_window != nullptr);SDL_SetWindowOpacity(_window, opacity);}void Hide(){SDL_HideWindow(_window);}void Show(){SDL_ShowWindow(_window);}SDL_Window* _window;};//Window}//sdl2#endif // SDL_WINDOW_HPP_INCLUDED
这篇关于第15章 《乐趣》Page305~311犯的错误,窗口GetID()为0的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!