SDL2小作业:使用原始函数绘图&使用ViewPort方法来绘制不同窗口的纹理
Lesson 8 Geometry Rendering
要求
使用SDL2库自带的绘图函数来绘制简单的几何形状,包括填充方法、非填充方法、虚线、实线等
运行结果
代码
#include <cstdio>
#include "SDL2/SDL.h"
// CONSTS
const int SCREEN_HEIGHT = 480;
const int SCREEN_WIDTH = 640;
SDL_Window* gWindow = nullptr;//in window
SDL_Renderer* gRenderer = nullptr;//global renderer
void freeSDL();
bool initWindow();
bool initWindow()
{
bool flag = true;
if (SDL_Init(SDL_INIT_VIDEO)<0)
{
printf("Unable to init Video!ERROR: %s\n",SDL_GetError());
flag = false;
}
else
{
gWindow= SDL_CreateWindow("SDLSample", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == nullptr)
{
printf("Unable to creatwindow!\nERROR:%s",SDL_GetError());
flag = false;
}
else
{
gRenderer = SDL_CreateRenderer(gWindow,-1,SDL_RENDERER_ACCELERATED);
if (gRenderer== nullptr)
{
printf("Renderer could not be created! ERROR:%s", SDL_GetError());
flag = false;
} else
{
SDL_SetRenderDrawColor(gRenderer,0XFF,0XFF,0XFF,0XFF);
}
}
}
return flag;
}
void freeSDL()
{
SDL_DestroyRenderer(gRenderer);
gRenderer = nullptr;
SDL_DestroyWindow(gWindow);
gWindow = nullptr;
SDL_Quit();
}
int main() {
if (!initWindow())
{
printf("unable to init window!\n");
}
else
{
bool quit = false;
SDL_Event event ;
while (!quit)
{
while (SDL_PollEvent(&event)!=0)
{
if (event.type==SDL_QUIT)
{
quit = true;
}
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear(gRenderer);
SDL_Rect fillRect = {SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2};
SDL_SetRenderDrawColor(gRenderer,0XFF,0X00,0X00,0XFF);
SDL_RenderFillRect(gRenderer,&fillRect);
SDL_Rect outlineRect = {SCREEN_WIDTH / 6, SCREEN_HEIGHT / 6, SCREEN_WIDTH* 2/3, SCREEN_HEIGHT * 2/3};
SDL_SetRenderDrawColor(gRenderer,0X00,0XFF,0X00,0XFF);
SDL_RenderDrawRect(gRenderer,&outlineRect);
SDL_SetRenderDrawColor( gRenderer, 0x00, 0x00, 0xFF, 0xFF );
SDL_RenderDrawLine( gRenderer, 0, SCREEN_HEIGHT / 2, SCREEN_WIDTH, SCREEN_HEIGHT / 2 );
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0x00, 0xFF );
for (int i = 0; i < SCREEN_HEIGHT; i+=4) {
SDL_RenderDrawPoint(gRenderer,SCREEN_WIDTH/2,i);
}
SDL_RenderPresent(gRenderer);
}
}
}
freeSDL();
return 0;
}
Lesson9 The Viewport
要求
基于L9的实现,渲染器将窗口渲染为两个ViewPort并且显示相同的图片。
运行结果
代码
#include <cstdio>
#include "SDL2/SDL.h"
#include "SDL2/SDL_image.h"
#include <string>
using std::string;
// CONSTS
const int SCREEN_HEIGHT = 480;
const int SCREEN_WIDTH = 640;
SDL_Window* gWindow = nullptr;//in window
SDL_Renderer* gRenderer = nullptr;//global renderer
SDL_Texture* gTexture = nullptr;
void freeSDL();
bool initWindow();
bool loadMedia();
SDL_Texture* loadTexture(const string& path);
bool initWindow()
{
bool flag = true;
if (SDL_Init(SDL_INIT_VIDEO)<0)
{
printf("Unable to init Video!ERROR: %s\n",SDL_GetError());
flag = false;
}
else
{
gWindow= SDL_CreateWindow("SDLSample", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == nullptr)
{
printf("Unable to creatwindow!\nERROR:%s",SDL_GetError());
flag = false;
}
else
{
gRenderer = SDL_CreateRenderer(gWindow,-1,SDL_RENDERER_ACCELERATED);
if (gRenderer== nullptr)
{
printf("Renderer could not be created! ERROR:%s", SDL_GetError());
flag = false;
} else
{
//SDL_SetRenderDrawColor(gRenderer,0XFF,0XFF,0XFF,0XFF);
auto IMG_FLAG = IMG_INIT_PNG;
if (!(IMG_Init(IMG_FLAG) & IMG_FLAG)){
printf("Unable to init Image!\n ERROR:%s",SDL_GetError());
flag =flag;
}
}
}
}
return flag;
}
SDL_Texture* loadTexture(const string& path)
{
SDL_Texture *tmpTexture = nullptr;
SDL_Surface* loadedSurface = IMG_Load(path.c_str());
if (loadedSurface == nullptr)
{
printf("Unable to load the Surface!\n ERROR:%s",SDL_GetError());
} else
{
tmpTexture = SDL_CreateTextureFromSurface(gRenderer,loadedSurface);
if (tmpTexture == nullptr)
{
printf("Unable to CreateTextureFromSurface! SDL_image Error: %s\n", SDL_GetError());
}
}
SDL_FreeSurface(loadedSurface);
return tmpTexture;
}
bool loadMedia()
{
auto flag = true;
gTexture = loadTexture("/home/wenqi/CLionProjects/SDLGame/screenshot.png");
if (gTexture == nullptr)
{
printf("Unable to load the Media!\n ERROR:%s",SDL_GetError());
flag = false;
}
return flag;
}
void freeSDL()
{
SDL_DestroyRenderer(gRenderer);
gRenderer = nullptr;
SDL_DestroyWindow(gWindow);
gWindow = nullptr;
SDL_DestroyTexture(gTexture);
gTexture = nullptr;
SDL_Quit();
IMG_Quit();
}
int main() {
if (!initWindow())
{
printf("unable to init window!\n");
}
else
{
if (!loadMedia())
{
printf("unable to loadMedia main!");
}
else
{
bool quit = false;
SDL_Event event ;
while (!quit)
{
while (SDL_PollEvent(&event)!=0)
{
if (event.type==SDL_QUIT)
{
quit = true;
}
SDL_SetRenderDrawColor(gRenderer,0XFF,0XFF,0XFF,0XFF);
SDL_RenderClear(gRenderer);
SDL_Rect topViewPort = {0,0,SCREEN_WIDTH,SCREEN_HEIGHT/2};
SDL_RenderSetViewport(gRenderer,&topViewPort);
SDL_RenderCopy(gRenderer,gTexture, nullptr, nullptr);
SDL_Rect BottomViewPort = {0,SCREEN_HEIGHT/2,SCREEN_WIDTH,SCREEN_HEIGHT/2};
SDL_RenderSetViewport(gRenderer,&BottomViewPort);
SDL_RenderCopy(gRenderer,gTexture, nullptr, nullptr);
SDL_RenderPresent(gRenderer);
}
}
}
}
freeSDL();
return 0;
}