使用SDL加载多张图片(二)

本文介绍了使用SDL和SDL_image库加载和渲染不同格式图片的方法,包括配置环境、下载依赖库、实现代码示例及效果展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://lazyfoo.net/SDL_tutorials/index.php
这是官网的教程,英语还行的同学可以去看这个,例子说的都很详细噢~

上篇说的图片为甚么加载不出来的问题终于解决了~
尝试了绝对路径,是可以加载图片的,于是我又把图片移到了当前的project根目录下。于是,图片终于显示出来了,很兴奋呢!

由于SDL本身的库只支持BMP格式的图片加载,可能我们的需求远远不止这些,还需要支持更多的图片格式,所以得下载基于SDL的extension library ----> SDL_image,本来长期做JAVA开发,理解起来就是一个插件的问题吧,虽然说法不是很严谨,哈哈。

下载外部库以及配置eclipse的具体步骤,参照http://lazyfoo.net/SDL_tutorials/lesson03/windows/eclipse/index.php
当中也有提到SDL_ttf,也是一个基于SDL的extension library,作用是字体表现,以后将会写到。

好了,博主的环境已经按照上面的步骤配置好了,SDL的头文件中已经包含了,SDL_image.h这个文件,lib文件和DLL文件也放入了相应的文件夹下。特别提醒:作者的SDL版本为1.2.15,所以下载的外部库,必须是1.2.15以下的才行,否则会不兼容。

这是作者目前项目的目录结构:

跟上一篇不一样了吧~
其中的SDL_fun.h中的函数是关于每个demo的一个封装,看看下面的代码便知晓了。

/*
 * SDL_fun.h
 *
 *  Created on: 2013-10-1
 *      Author: Christian
 */

#ifndef SDL_FUN_H_
#define SDL_FUN_H_

#include <iostream>
#include <SDL/SDL.h>

void drawBitmap(const char* fileName);
void readKeyboard(void);
void getRadioInfo(void);
void renderPictures(const char* fgFile, const char* bgFile);


#endif /* SDL_FUN_H_ */

SDL_fun.cpp:
以上函数的实现:

/*
 * SDL_fun.cpp
 *
 *  Created on: 2013-10-1
 *      Author: Christian
 */

#include <SDL/SDL_image.h>
#include "include/SDL_fun.h"

/** 将图片load到屏幕上 */
void drawBitmap(const char* fileName) {
//.........................
}

/** 读取按键的值  */
void readKeyboard() {
//.........................
}

/** 获取视频信息 */
void getRadioInfo(void) {
//.........................
}

/** 渲染两张图片,前景和背景 */
void renderPictures(const char* fgFile, const char* bgFile) {

	SDL_Surface* screen = NULL;
	SDL_Surface* forground = NULL;
	SDL_Surface* background = NULL;
	//全屏、双缓冲、使用显卡加载
	screen = SDL_SetVideoMode(800, 600, 32, SDL_FULLSCREEN | SDL_DOUBLEBUF | SDL_HWSURFACE);
	//forground = SDL_LoadBMP(fgFile);
	//background = SDL_LoadBMP(bgFile);
	forground = IMG_Load(fgFile);
	background = IMG_Load(bgFile);

	if (screen == 0 || forground == 0 || background == 0) {
		std::cerr << SDL_GetError() << " 133 \n";
	} else {

		SDL_Surface* fg = SDL_DisplayFormat(forground);
		SDL_Surface* bg = SDL_DisplayFormat(background);

		bool quit = false;
		SDL_Event event;
		SDL_Rect offset;
		offset.x = fg->w;
		offset.y = 0;

		while (!quit) {

			SDL_ClearError();
			if (SDL_PollEvent(&event) != 0 && event.type == SDL_QUIT) {
				quit = true;
			}
			//PICS Render!
			if (SDL_BlitSurface(fg, NULL, screen, NULL) != 0) {
				std::cerr << SDL_GetError() << " 155 \n";
				break;
			}

			if (SDL_BlitSurface(bg, NULL, screen, &offset) != 0) {
				std::cerr << SDL_GetError() << " 160 \n";
				break;
			}
			if (SDL_Flip(screen) != 0) {
				std::cerr << SDL_GetError() << " 164\n";
				break;
			}

		}

		SDL_FreeSurface(fg);
		SDL_FreeSurface(bg);
	}

	SDL_FreeSurface(forground);
	SDL_FreeSurface(background);
}

由于加入了SDL_image库,所以加载图片由SDL_LoadBMP函数变成了IMG_Load,实现了不同格式图片的加载。

然后在入口处调用就行了

//============================================================================
// Name        : SDL_demo.cpp
// Author      : Christian
// Version     :
// Copyright   : Copyright (c) Christian 2012/9/28
// Description : Hello World in C++, Ansi-style
//============================================================================

#include "include/SDL_init.h"
#include "include/SDL_fun.h"

using namespace std;

int main(int argc, char* args[]) {
	//SDL_putenv("SDL_VIDEODRIVER=directx");
	//putenv("SDL_VIDEODRIVER=directx");

	createSDL("Christian Test");

	//drawBitmap("3.jpg");
	//readKeyboard();
	//getRadioInfo();
	renderPictures("3.jpg", "2.bmp");

	atexit(destorySDL);

	return EXIT_SUCCESS;
}

下面是编译链接运行后的效果图:

其实是全屏的哟,只是截了一部分作为参考~有什么问题大家可以给我留言,一起交流交流。

这里是Freestyletime@foxmail.com,欢迎交流。

本人原创作品,转载请标明出处。

 

转载于:https://my.oschina.net/freestyletime/blog/165569

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值