SDL2.0_02_functions

 

#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;
	SDL_Surface *loadImage = SDL_LoadBMP(file.c_str());
	if (loadImage != nullptr) {
		tex = SDL_CreateTextureFromSurface(ren, loadImage);
		SDL_FreeSurface(loadImage);
	}
	else {
		cout << SDL_GetError() << 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.bmp");
	SDL_Texture *fg = LoadImage("F:/material/m_bottle.bmp");

	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)