#include <iostream>
#include <SDL.h>
#include <windows.h>
/*
* Lesson 0: Test to make sure SDL is setup properly
*/
//#define LESSON_5
//#define LESSON_4
//#define LESSON_3
//#define LESSON_2
//#define LESSON_1
//#define LESSON_0
#ifdef LESSON_5
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
using namespace std;
/*
* Lesson 0: Test to make sure SDL is setup properly
*/
#undef main
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//We‘ll just be using square tiles for now
const int TILE_SIZE = 40;
void logSDLError(std::ostream &os, const std::string &msg)
{
os << msg << " error: " << SDL_GetError() << std::endl;
}
SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren){
SDL_Texture *texture = IMG_LoadTexture(ren, file.c_str());
if (texture == nullptr){
logSDLError(std::cout, "LoadTexture");
}
return texture;
}
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, int w, int h){
//Setup the destination rectangle to be at the position we want
SDL_Rect dst;
dst.x = x;
dst.y = y;
dst.w = w;
dst.h = h;
SDL_RenderCopy(ren, tex, NULL, &dst);
}
static SDL_Rect clips[4] =
{
{0,0,100,100},
{100,0,100,100},
{0,100,100,100},
{100,100,100,100},
};
void render_clip(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, int w, int h,SDL_Rect *src)
{
//Setup the destination rectangle to be at the position we want
SDL_Rect dst;
if(!src)
return;
dst.x = x;
dst.y = y;
dst.w = w;
dst.h = h;
SDL_RenderCopy(ren, tex, src, &dst);
}
int main(int, char**){
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
logSDLError(std::cout, "SDL_Init");
return 1;
}
SDL_Window *window = SDL_CreateWindow("Lesson 2", 100, 100, SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == nullptr){
logSDLError(std::cout, "CreateWindow");
SDL_Quit();
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr){
logSDLError(std::cout, "CreateRenderer");
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
SDL_Texture* background = loadTexture("d:/background.png",renderer);
SDL_Texture* image = loadTexture("d:/image (1).png",renderer);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
int i = 0,j ,k,n = 0;
SDL_Event e;
bool quit = false;
while (!quit){
while (SDL_PollEvent(&e)){
if (e.type == SDL_QUIT){
quit = true;
}
if (e.type == SDL_KEYDOWN){
//switch (event.key.keysym.sym)
switch(e.key.keysym.sym)
{
case SDLK_ESCAPE:
quit = true;
break;
case SDLK_SPACE:
case SDLK_r:
i++;
n++;
break;
default:
break;
}
}
if (e.type == SDL_MOUSEBUTTONDOWN){
//quit = true;
}
}
SDL_RenderClear(renderer);
for(j = 0; j < (SCREEN_HEIGHT /30)+1; j++)
{
for(k = 0; k< (SCREEN_WIDTH /30)+1; k++)
{
renderTexture(background,renderer,k*30,j*30,30,30);
}
}
render_clip(image,renderer,i*30,i*30,100,100,&(clips[n%4]));
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(background);
SDL_DestroyTexture(image);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
std::cout<<"ok"<<std::endl;
return 0;
}
#endif
#ifdef LESSON_4
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
using namespace std;
/*
* Lesson 0: Test to make sure SDL is setup properly
*/
#undef main
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//We‘ll just be using square tiles for now
const int TILE_SIZE = 40;
void logSDLError(std::ostream &os, const std::string &msg)
{
os << msg << " error: " << SDL_GetError() << std::endl;
}
SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren){
SDL_Texture *texture = IMG_LoadTexture(ren, file.c_str());
if (texture == nullptr){
logSDLError(std::cout, "LoadTexture");
}
return texture;
}
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, int w, int h){
//Setup the destination rectangle to be at the position we want
SDL_Rect dst;
dst.x = x;
dst.y = y;
dst.w = w;
dst.h = h;
SDL_RenderCopy(ren, tex, NULL, &dst);
}
int main(int, char**){
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
logSDLError(std::cout, "SDL_Init");
return 1;
}
SDL_Window *window = SDL_CreateWindow("Lesson 2", 100, 100, SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == nullptr){
logSDLError(std::cout, "CreateWindow");
SDL_Quit();
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr){
logSDLError(std::cout, "CreateRenderer");
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
SDL_Texture* background = loadTexture("d:/background.png",renderer);
SDL_Texture* image = loadTexture("d:/image.png",renderer);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
int i = 0,j ,k;
SDL_Event e;
bool quit = false;
while (!quit){
while (SDL_PollEvent(&e)){
if (e.type == SDL_QUIT){
quit = true;
}
if (e.type == SDL_KEYDOWN){
//switch (event.key.keysym.sym)
switch(e.key.keysym.sym)
{
case SDLK_ESCAPE:
quit = true;
break;
case SDLK_SPACE:
case SDLK_r:
i++;
break;
default:
break;
}
}
if (e.type == SDL_MOUSEBUTTONDOWN){
//quit = true;
}
}
SDL_RenderClear(renderer);
for(j = 0; j < (SCREEN_HEIGHT /30)+1; j++)
{
for(k = 0; k< (SCREEN_WIDTH /30)+1; k++)
{
renderTexture(background,renderer,k*30,j*30,30,30);
}
}
renderTexture(image,renderer,i*30,i*30,100,100);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(background);
SDL_DestroyTexture(image);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
std::cout<<"ok"<<std::endl;
return 0;
}
#endif
#ifdef LESSON_3
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
using namespace std;
/*
* Lesson 0: Test to make sure SDL is setup properly
*/
#undef main
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//We‘ll just be using square tiles for now
const int TILE_SIZE = 40;
void logSDLError(std::ostream &os, const std::string &msg)
{
os << msg << " error: " << SDL_GetError() << std::endl;
}
SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren){
SDL_Texture *texture = IMG_LoadTexture(ren, file.c_str());
if (texture == nullptr){
logSDLError(std::cout, "LoadTexture");
}
return texture;
}
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, int w, int h){
//Setup the destination rectangle to be at the position we want
SDL_Rect dst;
dst.x = x;
dst.y = y;
dst.w = w;
dst.h = h;
SDL_RenderCopy(ren, tex, NULL, &dst);
}
int main(int, char**){
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
logSDLError(std::cout, "SDL_Init");
return 1;
}
SDL_Window *window = SDL_CreateWindow("Lesson 2", 100, 100, SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == nullptr){
logSDLError(std::cout, "CreateWindow");
SDL_Quit();
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr){
logSDLError(std::cout, "CreateRenderer");
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
SDL_Texture* background = loadTexture("d:/background.png",renderer);
SDL_Texture* image = loadTexture("d:/image.png",renderer);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
int i,j ,k;
for (i = 0; i < 30; ++i)
{
for(j = 0; j < (SCREEN_HEIGHT /30)+1; j++)
{
for(k = 0; k< (SCREEN_WIDTH /30)+1; k++)
{
renderTexture(background,renderer,k*30,j*30,30,30);
}
}
renderTexture(image,renderer,i*30,i*30,100,100);
SDL_RenderPresent(renderer);
SDL_Delay(1000);
}
SDL_DestroyTexture(background);
SDL_DestroyTexture(image);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
std::cout<<"ok"<<std::endl;
return 0;
}
#endif
#ifdef LESSON_2
#include <iostream>
#include <SDL.h>
using namespace std;
/*
* Lesson 0: Test to make sure SDL is setup properly
*/
#undef main
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
void logSDLError(std::ostream &os, const std::string &msg){
os << msg << " error: " << SDL_GetError() << std::endl;
}
SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren)
{
if(!ren)
return NULL;
SDL_Surface *bmp = SDL_LoadBMP(file.c_str());
if(bmp == nullptr)
{
logSDLError(cout, "SDL_LoadBMP");
return NULL;
}
SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
SDL_FreeSurface(bmp);
if(tex == nullptr)
{
logSDLError(cout, "SDL_CreateTextureFromSurface");
return NULL;
}
return tex;
}
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y){
//Setup the destination rectangle to be at the position we want
SDL_Rect dst;
dst.x = x;
dst.y = y;
//Query the texture to get its width and height to use
SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);
SDL_RenderCopy(ren, tex, NULL, &dst);
}
int main(int, char**){
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
logSDLError(std::cout, "SDL_Init");
return 1;
}
SDL_Window *window = SDL_CreateWindow("Lesson 2", 100, 100, SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == nullptr){
logSDLError(std::cout, "CreateWindow");
SDL_Quit();
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr){
logSDLError(std::cout, "CreateRenderer");
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
SDL_Texture *background = loadTexture("d:/background.bmp",renderer);
SDL_Texture *image = loadTexture("d:/image.bmp",renderer);
SDL_RenderClear(renderer);
renderTexture(background,renderer,20,20);
for (int i = 0; i < 30; ++i)
{
//SDL_RenderClear(renderer);
//renderTexture(background,renderer,20,20);
renderTexture(image,renderer,20+30*i,20+30*i);
SDL_RenderPresent(renderer);
SDL_Delay(1000);
}
SDL_DestroyTexture(background);
SDL_DestroyTexture(image);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
std::cout<<"ok"<<std::endl;
return 0;
}
#endif
#ifdef LESSON_1
#include <iostream>
#include <SDL.h>
using namespace std;
/*
* Lesson 0: Test to make sure SDL is setup properly
*/
#undef main
int main(int, char**)
{
if(SDL_Init(SDL_INIT_VIDEO) != 0)
{
cout<<"SDL_Init Error:"<<SDL_GetError()<<endl;
return 1;
}
SDL_Window *win = SDL_CreateWindow("Hello world!",100,100,640,480,SDL_WINDOW_SHOWN);
if(win == nullptr)
{
cout<<"SDL_CreateWindow Error:"<<SDL_GetError()<<endl;
SDL_Quit();
return 1;
}
SDL_Renderer *ren = SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if(ren == nullptr)
{
SDL_DestroyWindow(win);
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
string imagepath = string("d:/")+"hello.bmp";
SDL_Surface *bmp = SDL_LoadBMP(imagepath.c_str());
if (bmp == nullptr){
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
SDL_FreeSurface(bmp);
if (tex == nullptr){
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
for (int i = 0; i < 3; ++i){
//First clear the renderer
cout<<"before call SDL_RenderClear(ren)"<<endl;
SDL_Delay(2000);
SDL_RenderClear(ren); //这里的ren指的是"后台缓存",把它清掉,不影响前台显示。
SDL_RenderPresent(ren);//这里会把后台缓存更新到前台,所以显示黑色窗口了。
cout<<"after call SDL_RenderClear(ren)"<<endl;
SDL_Delay(2000);
cout<<"before call SDL_RenderCopy"<<endl;
SDL_Delay(2000);
//Draw the texture
SDL_RenderCopy(ren, tex, NULL, NULL); // 这里是把SDL_Texture 的整个部分copy到ren
cout<<"after call SDL_RenderCopy"<<endl;
SDL_Delay(2000);
//Update the screen
cout<<"before call SDL_RenderPresent"<<endl;
SDL_Delay(2000);
SDL_RenderPresent(ren);
cout<<"after call SDL_RenderPresent"<<endl;
SDL_Delay(2000);
//Take a quick break after all that hard work
SDL_Delay(1000);
}
Sleep(1000);
SDL_DestroyTexture(tex);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
}
#endif
#ifdef LESSON_0
#include <iostream>
#include <SDL.h>
/*
* Lesson 0: Test to make sure SDL is setup properly
*/
#undef main
int main(int, char**){
if (SDL_Init(SDL_INIT_VIDEO) != 0){
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Quit();
std::cout<<"ok"<<std::endl;
return 0;
}
#endif