【c++|SDL】二、读取图片、显示图片、动画制作

every blog every motto: You can do more than you think.
https://blog.csdn.net/weixin_39190382?type=blog

0. 前言

读取图片,显示图片,动画


SDL中有两种在屏幕上显示的方法

  • SDL_Surface: 使用软件渲染处理
  • SDL_Texture: 使用硬件加速渲染处理

1. 在界面上显示图片

修改Game.h

#ifndef __Game__
#define __Game__#include <SDL2/SDL.h>
class Game
{public:Game() {};~Game() {};// simply set the running variable to truebool init(const char* title, int xpos, int ypos,int width, int height, bool fullscreen);void render();void update();void handleEvents();void clean();// a function to access the private running variablebool running() { return m_bRunning; }
private:bool m_bRunning;// SDL_Window* m_pWindow;// SDL_Renderer* m_pRenderer;SDL_Window* m_pWindow;SDL_Renderer* m_pRenderer;SDL_Texture* m_pTexture; // the new SDL_Texture variableSDL_Rect m_sourceRectangle; // the first rectangleSDL_Rect m_destinationRectangle; // another rectangle
};#endif /* defined(__Game__) */

修改Game.cpp

修改其中的init和render函数

#include "Game.h"
#include <iostream>using namespace std;bool Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
{   int flags = 0;if (fullscreen){flags = SDL_WINDOW_FULLSCREEN;}// attempt to initialize SDLif(SDL_Init(SDL_INIT_EVERYTHING) == 0){std::cout << "SDL init success\n";// init the windowm_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);if(m_pWindow != 0) // window init success{std::cout << "window creation success\n";m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);if(m_pRenderer != 0) // renderer init success{std::cout << "renderer creation success\n";SDL_SetRenderDrawColor(m_pRenderer,255,255,255,255);}else{std::cout << "renderer init fail\n";return false; // renderer init fail}}else{std::cout << "window init fail\n";return false; // window init fail}}else{std::cout << "SDL init fail\n";return false; // SDL init fail}std::cout << "init success\n";SDL_Surface* pTempSurface = SDL_LoadBMP("assets/rider.bmp");m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer, pTempSurface);SDL_FreeSurface(pTempSurface);SDL_QueryTexture(m_pTexture, NULL, NULL,&m_sourceRectangle.w,&m_sourceRectangle.h); // 获取长宽m_destinationRectangle.x = m_sourceRectangle.x = 0;m_destinationRectangle.y = m_sourceRectangle.y = 0;m_destinationRectangle.w = m_sourceRectangle.w;m_destinationRectangle.h = m_sourceRectangle.h;m_bRunning = true; // everything inited successfully, start the main loopreturn true;
}void Game::render()
{SDL_RenderClear(m_pRenderer); // clear the renderer to the draw colorSDL_RenderCopy(m_pRenderer, m_pTexture,&m_sourceRectangle,&m_destinationRectangle);SDL_RenderPresent(m_pRenderer); // draw to the screen
}void Game::handleEvents()
{SDL_Event event;if(SDL_PollEvent(&event)){switch (event.type){case SDL_QUIT:m_bRunning = false;break;default:break;}}
}void Game::clean()
{std::cout << "cleaning game\n";SDL_DestroyWindow(m_pWindow);SDL_DestroyRenderer(m_pRenderer);SDL_Quit();
}void Game::update()
{}

在这里插入图片描述

2. 显示位置和范围

修改m_sourceRectangle和m_destinationRectangle
具体在Game.cpp SDL_QueryTexture代码下面

Game.cpp init函数代码如下,其他代码相同

bool Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
{   int flags = 0;if (fullscreen){flags = SDL_WINDOW_FULLSCREEN;}// attempt to initialize SDLif(SDL_Init(SDL_INIT_EVERYTHING) == 0){std::cout << "SDL init success\n";// init the windowm_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);if(m_pWindow != 0) // window init success{std::cout << "window creation success\n";m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);if(m_pRenderer != 0) // renderer init success{std::cout << "renderer creation success\n";SDL_SetRenderDrawColor(m_pRenderer,255,255,255,255);}else{std::cout << "renderer init fail\n";return false; // renderer init fail}}else{std::cout << "window init fail\n";return false; // window init fail}}else{std::cout << "SDL init fail\n";return false; // SDL init fail}std::cout << "init success\n";SDL_Surface* pTempSurface = SDL_LoadBMP("assets/rider.bmp");m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer, pTempSurface);SDL_FreeSurface(pTempSurface);SDL_QueryTexture(m_pTexture, NULL, NULL,&m_sourceRectangle.w,&m_sourceRectangle.h); // 获取长宽m_sourceRectangle.w = 50;m_sourceRectangle.h = 50;m_destinationRectangle.x = 100;m_destinationRectangle.y = 100;m_sourceRectangle.x = 50;m_sourceRectangle.y = 50;// m_destinationRectangle.x = m_sourceRectangle.x = 0;// m_destinationRectangle.y = m_sourceRectangle.y = 0;m_destinationRectangle.w = m_sourceRectangle.w;m_destinationRectangle.h = m_sourceRectangle.h;m_bRunning = true; // everything inited successfully, start the main loopreturn true;
}

在这里插入图片描述

3. 动图

有多帧图片,每100ms显示其中一张,可以让图片动起来
在这里插入图片描述

#include "Game.h"
#include <iostream>using namespace std;bool Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
{   int flags = 0;if (fullscreen){flags = SDL_WINDOW_FULLSCREEN;}// attempt to initialize SDLif(SDL_Init(SDL_INIT_EVERYTHING) == 0){std::cout << "SDL init success\n";// init the windowm_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);if(m_pWindow != 0) // window init success{std::cout << "window creation success\n";m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);if(m_pRenderer != 0) // renderer init success{std::cout << "renderer creation success\n";SDL_SetRenderDrawColor(m_pRenderer,255,255,255,255);}else{std::cout << "renderer init fail\n";return false; // renderer init fail}}else{std::cout << "window init fail\n";return false; // window init fail}}else{std::cout << "SDL init fail\n";return false; // SDL init fail}std::cout << "init success\n";// SDL_Surface* pTempSurface = SDL_LoadBMP("assets/rider.bmp");SDL_Surface* pTempSurface = SDL_LoadBMP("assets/animate.bmp");m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer, pTempSurface);SDL_FreeSurface(pTempSurface);SDL_QueryTexture(m_pTexture, NULL, NULL,&m_sourceRectangle.w,&m_sourceRectangle.h); // 获取长宽m_sourceRectangle.x = 0;m_sourceRectangle.y = 0;m_sourceRectangle.w = 128;m_sourceRectangle.h = 82;m_destinationRectangle.x = 0;m_destinationRectangle.y = 0;// m_destinationRectangle.x = m_sourceRectangle.x = 0;// m_destinationRectangle.y = m_sourceRectangle.y = 0;m_destinationRectangle.w = m_sourceRectangle.w;m_destinationRectangle.h = m_sourceRectangle.h;m_bRunning = true; // everything inited successfully, start the main loopreturn true;
}void Game::render()
{SDL_RenderClear(m_pRenderer); // clear the renderer to the draw color// SDL_RenderCopy(m_pRenderer, m_pTexture,0,0);SDL_RenderCopy(m_pRenderer, m_pTexture,&m_sourceRectangle,&m_destinationRectangle);SDL_RenderPresent(m_pRenderer); // draw to the screen
}void Game::handleEvents()
{SDL_Event event;if(SDL_PollEvent(&event)){switch (event.type){case SDL_QUIT:m_bRunning = false;break;default:break;}}
}void Game::clean()
{std::cout << "cleaning game\n";SDL_DestroyWindow(m_pWindow);SDL_DestroyRenderer(m_pRenderer);SDL_Quit();
}void Game::update()
{m_sourceRectangle.x = 128 * int(((SDL_GetTicks() / 100) % 6));}

在这里插入图片描述

可以翻转图片

void Game::render()
{SDL_RenderClear(m_pRenderer); // clear the renderer to the draw color// SDL_RenderCopy(m_pRenderer, m_pTexture,0,0);// SDL_RenderCopy(m_pRenderer, m_pTexture,&m_sourceRectangle,&m_destinationRectangle);SDL_RenderCopyEx(m_pRenderer, m_pTexture,&m_sourceRectangle, &m_destinationRectangle,0, 0, SDL_FLIP_HORIZONTAL); // pass in the horizontal flipSDL_RenderPresent(m_pRenderer); // draw to the screen
}

3. SDL_image

前面用的是SDL_LoadBMP只能加载bmp图片,SDL_image可以加载很多格式的图片,包括png,jpg,bmp,tiff,pcx,tif,lbm,xpm,gif,tk和xv等。

Game.h中引入SDL_image

#include <SDL2/SDL_image.h>

修改加载图片代码Game.cpp

SDL_Surface* pTempSurface = IMG_Load("assets/animate.png");

编译修改为:

g++ m14main.cpp Game.cpp  -o m14 -lSDL2 -lSDL2_image

Game.cpp代码如下:

#include "Game.h"
#include <iostream>using namespace std;bool Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
{   int flags = 0;if (fullscreen){flags = SDL_WINDOW_FULLSCREEN;}// attempt to initialize SDLif(SDL_Init(SDL_INIT_EVERYTHING) == 0){std::cout << "SDL init success\n";// init the windowm_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);if(m_pWindow != 0) // window init success{std::cout << "window creation success\n";m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);if(m_pRenderer != 0) // renderer init success{std::cout << "renderer creation success\n";SDL_SetRenderDrawColor(m_pRenderer,255,0,0,255);}else{std::cout << "renderer init fail\n";return false; // renderer init fail}}else{std::cout << "window init fail\n";return false; // window init fail}}else{std::cout << "SDL init fail\n";return false; // SDL init fail}std::cout << "init success\n";// SDL_Surface* pTempSurface = SDL_LoadBMP("assets/rider.bmp");// SDL_Surface* pTempSurface = SDL_LoadBMP("assets/animate.bmp");// SDL_Surface* pTempSurface = IMG_Load("assets/animate.png");SDL_Surface* pTempSurface = IMG_Load("assets/animate-alpha.png");m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer, pTempSurface);SDL_FreeSurface(pTempSurface);SDL_QueryTexture(m_pTexture, NULL, NULL,&m_sourceRectangle.w,&m_sourceRectangle.h); // 获取长宽m_sourceRectangle.x = 0;m_sourceRectangle.y = 0;m_sourceRectangle.w = 128;m_sourceRectangle.h = 82;m_destinationRectangle.x = 0;m_destinationRectangle.y = 0;// m_destinationRectangle.x = m_sourceRectangle.x = 0;// m_destinationRectangle.y = m_sourceRectangle.y = 0;m_destinationRectangle.w = m_sourceRectangle.w;m_destinationRectangle.h = m_sourceRectangle.h;m_bRunning = true; // everything inited successfully, start the main loopreturn true;
}void Game::render()
{SDL_RenderClear(m_pRenderer); // clear the renderer to the draw color// SDL_RenderCopy(m_pRenderer, m_pTexture,0,0);// SDL_RenderCopy(m_pRenderer, m_pTexture,&m_sourceRectangle,&m_destinationRectangle);SDL_RenderCopyEx(m_pRenderer, m_pTexture,&m_sourceRectangle, &m_destinationRectangle,0, 0, SDL_FLIP_HORIZONTAL); // pass in the horizontal flipSDL_RenderPresent(m_pRenderer); // draw to the screen
}void Game::handleEvents()
{SDL_Event event;if(SDL_PollEvent(&event)){switch (event.type){case SDL_QUIT:m_bRunning = false;break;default:break;}}
}void Game::clean()
{std::cout << "cleaning game\n";SDL_DestroyWindow(m_pWindow);SDL_DestroyRenderer(m_pRenderer);SDL_Quit();
}void Game::update()
{m_sourceRectangle.x = 128 * int(((SDL_GetTicks() / 100) % 6));}

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/224860.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Windows10-用户账户控制、Windows远程桌面

Windows10用户账户控制怎么设置白名单 问题引出&#xff1a; 安装低版本搜狗输入法后经常弹出用户账户控制 解决方案&#xff1a; 全局模式&#xff1a; UAC控制最早出现在Windows Vista中&#xff0c;用户帐户控制&#xff08;UAC&#xff09;是一项旨在防止对您的计算机…

【网络安全】-常见的网站攻击方式详解

文章目录 介绍1. SQL 注入攻击攻击原理攻击目的防范措施 2. 跨站脚本攻击&#xff08;XSS&#xff09;攻击原理攻击目的防范措施 3. CSRF 攻击攻击原理攻击目的防范措施 4. 文件上传漏洞攻击原理攻击目的防范措施 5. 点击劫持攻击原理攻击目的防范措施 结论 介绍 在数字时代&a…

hello vtk 圆柱

VTK 可视化的流程及步骤 标题引入VTK库和初始化&#xff1a; 引入 VTK 库和 AutoInit 模块&#xff0c;以便使用 VTK 的渲染和交互功能 设置背景颜色和颜色对象&#xff1a; 使用 vtkNamedColors 设置背景颜色和演员颜色。 创建圆柱体源&#xff1a; 使用 vtkCylinderSou…

图书管理系统源码,图书管理系统开发,图书借阅系统源码四TuShuManager应用程序MVC控制器Controllers

Asp.net web应用程序MVC之Controllers控制器 Controller在ASP.NET MVC中负责控制所有客户端与服务器端的交互,并且负责协调Model与View之间的数据传递,是ASP.NET MVC的核心。 撰写Controller的基本要求: 1、Controller必须为公开类别; 2、Controller名称必须以Controller结…

【C++】哈希(位图、布隆过滤器)

一、哈希的应用&#xff08;位图和布隆过滤器&#xff09; 1、位图&#xff08;bitset&#xff09; &#xff08;1&#xff09;位图概念 【题目】 给 40亿 个不重复的无符号整数&#xff0c;没排过序。给一个无符号整数&#xff0c;如何快速判断一个数是否在这 40亿 个数中。…

UserWarning: FigureCanvasAgg is non-interactive, and thus cannot be shown

一个奇怪的BUG 这个代码会报下面的错&#xff1a; 但是把模型导入部分注释掉之后就没有这个错误了&#xff08;第六行&#xff09; 解决办法&#xff1a;在模型加载后面加入一行代码 matplotlib.use( TkAgg’)&#xff0c;这个bug的问题就是模型加载改变了matplotlib使用的终端…

【Dockerfile】将自己的项目构建成镜像部署运行

目录 1.Dockerfile 2.镜像结构 3.Dockerfile语法 4.构建Java项目 5.基于Java8构建项目 1.Dockerfile 常见的镜像在DockerHub就能找到&#xff0c;但是我们自己写的项目就必须自己构建镜像了。 而要自定义镜像&#xff0c;就必须先了解镜像的结构才行。 2.镜像结构 镜…

在VMcentos7上用docker部署SELKS(IDS系统)

基本安装所需环境&#xff1a; 2核&#xff08;至少&#xff09;10 GB 可用 RAM&#xff08;经测试&#xff0c;4GB也能运行但会卡&#xff09;至少 30 GB 可用磁盘空间&#xff08;实际磁盘占用情况主要取决于规则数量和网络流量&#xff09;。建议使用 200GB SSD 级别。git,…

【Linux】:信号在内核里的处理

信号的发送和保存 一.内核中的信号处理二.信号集操作函数1.一些信号函数2.sigprocmask3.sigpending4.写代码 三.信号在什么时候处理的四.再谈地址空间 一.内核中的信号处理 1.实际执行信号的处理动作称为信号递达(Delivery )2.信号从产生到递达之间的状态,称为信号未决(Pending…

深度学习18

卷积层 查看每个数据 使用tensorboard查看 池化层 使用数据集进行训练 创建实例&#xff0c;使用tensorboard进行显示 最大池化保留了图片信息&#xff0c;神经网络训练的数据量大大减小&#xff0c;可以加快训练 非线性激活 非线性激活为神经网络加入了一些非线性的特质…

【密码学】【安全多方计算】浅析隐私求交PSI

文章目录 隐私求交的定义隐私求交方案介绍1. 基于DH的PSI方案2. 基于OT的PSI方案3.基于OPRF的PSI方案 总结 隐私求交的定义 隐私集合求交使得持有数据参与方通过计算得到集合的交集数据&#xff0c;而不泄露任何交集以外的数据信息。 隐私求交方案介绍 1. 基于DH的PSI方案 …

linux查看emmc分区信息(10种方法 )

目录 ## 1 emmc ## 2 uboot查看 ## 3 kernel查看 方法1 方法2 方法3 方法4 方法5 方法6 方法7 方法8 方法9 方法10 ## 1 emmc 我们要说的是&#xff0c;User Data Partition中的再分区 可简化为 ## 2 uboot查看 u-boot> mmc partPartition Map for MMC device…