SDL学习二(显示png图片)

// SDLSample.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include <stdexcept>
#include <string>
#include <iostream>
#include "SDL.h"
#include "SDL_image.h"


//屏幕宽度
const int SCREEN_WIDTH = 1600;
const int SCREEN_HEIGHT = 900;


//全局窗口和渲染器
SDL_Window *window = nullptr;
SDL_Renderer *renderer = nullptr;


//加载图片
SDL_Texture* LoadImage(std::string file)
{
SDL_Texture* tex = nullptr;
tex = IMG_LoadTexture(renderer, file.c_str());
if (tex == nullptr)
throw std::runtime_error("Failed to load image: " + file + IMG_GetError());
return tex;
}


//将表面应用到渲染器
void ApplySurface(int x, int y, SDL_Texture *tex, SDL_Renderer *rend)
{
SDL_Rect pos;
pos.x = x;
pos.y = y;
SDL_QueryTexture(tex, NULL, NULL, &pos.w, &pos.h);
SDL_RenderCopy(rend, tex, NULL, &pos);
}


int main(int argc, char* argv[])
{
//初始化SDL
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
std::cout << SDL_GetError() << std::endl;
return 1;
}


//创建窗口
window = SDL_CreateWindow("Lesson 3",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == nullptr)
{
std::cout << SDL_GetError() << std::endl;
return 2;
}


//创建渲染器
renderer = SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr)
{
std::cout << SDL_GetError() << std::endl;
return 3;
}


//创建背景和前景纹理
SDL_Texture *background = nullptr, *image = nullptr;
try {
background = LoadImage("hello.png");
image = LoadImage("hello.png");
}
catch (const std::runtime_error &e) {
std::cout << e.what() << std::endl;
return 4;
}


//清空渲染器
SDL_RenderClear(renderer);


//在渲染器内平铺背景
int bW, bH;
SDL_QueryTexture(background, NULL, NULL, &bW, &bH);
for (int y = 0; y <= SCREEN_HEIGHT; y += bH)
for (int x = 0; x <= SCREEN_WIDTH; x += bW)
ApplySurface(x, y, background, renderer);




//在渲染器中央放置前景
int iW, iH;
SDL_QueryTexture(image, NULL, NULL, &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_Delay(20000);


//释放资源
SDL_DestroyTexture(background);
SDL_DestroyTexture(image);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);


SDL_Quit();


return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值