本篇内容接上篇文章,继续讲述本人使用SDL2复刻曹操传的过程。上次主要实现创建窗口并显示背景图,今次将完善游戏窗口的基本功能,如最大化、拉伸、最小化。然后渲染英雄单位到地图上。为方便窗口的管理,创建GameWindow对象:
#ifndef SDL_CCZ_GAMEWINDOW_H
#define SDL_CCZ_GAMEWINDOW_H
#include "stdbool.h"
#include "SDL2/SDL.h"
struct GameWindow
{
//Window data
SDL_Window* sdl_window;
SDL_Renderer* sdl_renderer;
// Window title
char caption[256];
//Window dimensions
int width;
int height;
//Window focus
bool mouse_focus;
bool keyboard_focus;
bool fullscreen;
bool minimized;
//Screen ticks per frame
int tpf;
};
bool gamewindow_init(struct GameWindow* ptr_gw);
void gamewindow_handle_event(struct GameWindow* ptr_gw, SDL_Event* e);
void gamewindow_free(struct GameWindow* ptr_gw);
#endif //SDL_CCZ_GAMEWINDOW_H
主要的功能函数:
void gamewindow_handle_event(struct GameWindow* ptr_gw, SDL_Event* e);
用于处理窗口事件,窗口事件的枚举值见下图:
窗口处理函数的实现:
void gamewindow_handle_event(struct GameWindow* ptr_gw, SDL_Event* e)
{
//Window event occured
if( e->type == SDL_WINDOWEVENT )
{
//Caption update flag
bool update_caption = false; //窗口标题更新
switch(e->window.event)
{
//Get new dimensions and repaint on window size change
case SDL_WINDOWEVENT_SIZE_CHANGED:
ptr_gw->width = e->window.data1;
ptr_gw->height = e->windo