// EventDemo.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "SDL.h"
#include "SDL_image.h"
#include <iostream>
#include <string>
//窗口属性
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
SDL_Texture *load_image(SDL_Renderer *renderer, const std::string filename)
{
SDL_Surface* loadedImage = nullptr;
SDL_Texture *texture = nullptr;
loadedImage = IMG_Load(filename.c_str());
if (loadedImage != nullptr)
{
texture = SDL_CreateTextureFromSurface(renderer, loadedImage);
SDL_FreeSurface(loadedImage);
}
else
{
std::cout << SDL_GetError() << std::endl;
}
return texture;
}
void ApplySurface(int x, int y, SDL_Texture *texture, SDL_Renderer *render)
{
SDL_Rect pos;
pos.x = x;
pos.y = y;
SDL_QueryTexture(texture, nullptr, nullptr, &pos.w, &pos.h);
SDL_RenderCopy(render, texture, nullptr, &pos);
}
int _EventDemo()
{
//表面
SDL_Texture *image = nullptr;
SDL_Window *screen = nullptr;
SDL_Renderer *renderer = nullptr;
SDL_Surface* gScreenSurface = nullptr;
SDL_Texture *background = nullptr;
bool quit = false;
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
std::cout << SDL_GetError() << std::endl;
}
screen = SDL_CreateWindow("Event Driven",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if (screen == nullptr)
{
std::cout << SDL_GetError() << std::endl;
}
//修改屏幕的背景颜色
gScreenSurface = SDL_GetWindowSurface(screen);
SDL_FillRect(gScreenSurface, nullptr, 0xCC9966);
renderer = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr)
{
std::cout << SDL_GetError() << std::endl;
return 1;
}
background = SDL_CreateTextureFromSurface(renderer, gScreenSurface); //从surface 转换到 texture
ApplySurface(0, 0, background, renderer); //利用渲染器将颜色绘制到窗口中
image = load_image(renderer, "background.png");
if (image == nullptr)
{
std::cout << SDL_GetError() << std::endl;
return 1;
}
int iW = 0, iH = 0;
SDL_QueryTexture(image, nullptr, nullptr, &iW, &iH);
int x = SCREEN_WIDTH / 2 - iW / 2;
int y = SCREEN_HEIGHT / 2 - iH / 2;
ApplySurface(x, y, image, renderer);
SDL_RenderPresent(renderer);
SDL_Event event;
while (quit == false) //当用户不想退出时
{
//函数SDL_PollEvent() 的作用是从事件队列底部取出一个事件并将其事件数据粘贴到一个SDL_Event类型的结构体中
while (SDL_PollEvent(&event))
{
//若用户点击量窗口右上角的关闭按钮
if (event.type == SDL_QUIT)
{
//退出程序
quit = true;
}
//键盘按键按下处理
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym) {
case SDLK_a:
quit = true;
break;
case SDLK_b:
std::cout << "keyboard b clicked" << std::endl;
break;
default:
break;
}
}
//鼠标三键按下处理
if (event.type == SDL_MOUSEBUTTONDOWN)
{
if (SDL_BUTTON_LEFT == event.button.button)
{
std::cout << " ========================== " << std::endl;
int px = event.button.x;
int py = event.button.y;
std::cout << " Left X position: " << px << std::endl;
std::cout << " Left Y position: " << py << std::endl << std::endl;
}
else if (SDL_BUTTON_RIGHT == event.button.button)
{
std::cout << " ========================== " << std::endl;
int px = event.button.x;
int py = event.button.y;
std::cout << " Right X position: " << px << std::endl;
std::cout << " Right Y position: " << py << std::endl << std::endl;
}
else if (SDL_BUTTON_MIDDLE == event.button.button)
{
std::cout << " ========================== " << std::endl;
int px = event.button.x;
int py = event.button.y;
std::cout << " Middle X position: " << px << std::endl;
std::cout << " Middle Y position: " << py << std::endl << std::endl;
}
}
}
}
SDL_DestroyTexture(image);
SDL_DestroyRenderer(renderer);
SDL_FreeSurface(gScreenSurface);
SDL_DestroyWindow(screen);
SDL_Quit();
return 0;
}