SDL2.0_03_SDL_image Library

扩展库SDL_Image,运行的时候需要把zlib1.dll,libjpeg-9.dll,libpng16-16.dll一同拷到目录里,不然不会报错,但就是运行没显示结果。

代码是在上一课的基础上略加修改的。

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

#include <SDL.h>
#include <SDL_image.h>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

SDL_Window *win = nullptr;
SDL_Renderer *ren = nullptr;

SDL_Texture * LoadImage(string file) {
	SDL_Texture *tex = nullptr;
	tex = IMG_LoadTexture(ren, file.c_str());
	if (tex == nullptr) {
		cout << "Fail to load image in function LoadImage()!" << endl;
	}

	return tex;
}

void ApplySurface(int x, int y, SDL_Texture *tex, SDL_Renderer *ren){
	SDL_Rect pos;
	pos.x = x;
	pos.y = y;
	SDL_QueryTexture(tex, NULL, NULL, &pos.w, &pos.h);
	SDL_RenderCopy(ren, tex, NULL, &pos);
}

int main(int argc, char** argv)
{
	if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
		cout << SDL_GetError() << endl;
		return 0;
	}

	win = SDL_CreateWindow("02:", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_RESIZABLE);

	if (win == nullptr) {
		cout << SDL_GetError() << endl;
		return 0;
	}
	
	ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
	if (ren == nullptr) {
		cout << SDL_GetError() << endl;
		return 0;
	}
	SDL_Texture *bg = LoadImage("f:/material/giantShadowFlash.jpg");
	SDL_Texture *fg = LoadImage("F:/material/bubble.png");

	if (bg == nullptr || fg == nullptr) {
		cout << "Could not load one of the images!" << endl;
		return 0;
	}

	SDL_RenderClear(ren);

	int bw, bh;
	SDL_QueryTexture(bg, NULL, NULL, &bw, &bh);
	ApplySurface(0, 0, bg, ren);
	ApplySurface(bw, 0, bg, ren);
	ApplySurface(0, bh, bg, ren);
	ApplySurface(bw, bh, bg, ren);

	SDL_QueryTexture(fg, NULL, NULL, &bw, &bh);
	ApplySurface(SCREEN_WIDTH / 2 - bw / 2, SCREEN_HEIGHT / 2 - bh / 2, fg, ren);

	SDL_RenderPresent(ren);
	SDL_Delay(2000);
	SDL_DestroyTexture(bg);
	SDL_DestroyTexture(fg);
	SDL_DestroyRenderer(ren);
	SDL_DestroyWindow(win);
	SDL_Quit();

	cout << "Success!" << endl;
	return 0;
}


 

 

 

你可能感兴趣的:(sdl)