Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL2/SDL.h"
- #include "SDL2/SDL_image.h"
- #include <stdio.h>
- #include <stdbool.h>
- #define SCREEN_WIDTH 640
- #define SCREEN_HEIGHT 480
- bool init();
- bool loadMedia();
- void end();
- SDL_Window *gWindow = NULL;
- SDL_Renderer *gRenderer = NULL;
- typedef struct lTexture
- {
- //The actual hardware texture
- SDL_Texture *mTexture;
- //Image dimensions
- int mWidth;
- int mHeight;
- } lTexture;
- lTexture gFooTexture;
- lTexture gBackgroundTexture;
- void AllocTexture(lTexture *texture)
- {
- //Initialize
- texture->mTexture = NULL;
- texture->mWidth = 0;
- texture->mHeight = 0;
- }
- void DeallocTexture(lTexture *texture)
- {
- //Deallocate
- if (texture->mTexture != NULL)
- {
- SDL_DestroyTexture(texture->mTexture);
- texture->mTexture = NULL;
- texture->mWidth = 0;
- texture->mHeight = 0;
- }
- }
- void render(int x, int y, lTexture *texture)
- {
- //Set render space and render to screen
- SDL_Rect renderQuad = {x, y, texture->mWidth, texture->mHeight};
- SDL_RenderCopy(gRenderer, texture->mTexture, NULL, &renderQuad);
- }
- bool loadFromFile(const char *path, lTexture *texture)
- {
- //Get rid of preexisting texture
- DeallocTexture(texture);
- //The final texture
- SDL_Texture *newTexture = NULL;
- //Load image at specified path
- SDL_Surface *loadedSurface = IMG_Load(path);
- if(loadedSurface == NULL)
- {
- printf("Unable to load image %s! SDL_image error: %s\n", path, IMG_GetError());
- }
- else
- {
- //Color key image
- SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format,
- 0x41, 0x80, 0xA5));
- //Create texture from surface pixels
- newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
- if(newTexture == NULL)
- {
- printf("Unable to create texture from %s! SDL Error: %s\n", path, SDL_GetError());
- }
- else
- {
- //Get image dimensions
- texture->mWidth = loadedSurface->w;
- texture->mHeight = loadedSurface->h;
- }
- //Get rid of old loaded surface
- SDL_FreeSurface(loadedSurface);
- }
- //Return success
- texture->mTexture = newTexture;
- return texture->mTexture != NULL;
- }
- int getWidth(lTexture *texture)
- {
- return texture->mWidth;
- }
- int getHeight(lTexture *texture)
- {
- return texture->mHeight;
- }
- bool init()
- {
- bool success = true;
- if(SDL_Init(SDL_INIT_VIDEO) < 0)
- {
- printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
- success = false;
- }
- else
- {
- //Set texture filtering to linear
- if(!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
- {
- printf("Warning: Linear texture filtering not enabled!\n");
- }
- //Create window
- gWindow = SDL_CreateWindow("SDL Tutorial",
- SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED,
- SCREEN_WIDTH,
- SCREEN_HEIGHT,
- SDL_WINDOW_SHOWN);
- if(gWindow == NULL)
- {
- printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
- success = false;
- }
- else
- {
- //Create renderer for window
- gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
- if(gRenderer == NULL)
- {
- printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
- success = false;
- }
- else
- {
- //Initialize renderer color
- SDL_SetRenderDrawColor(gRenderer,0xFF, 0xFF, 0xFF, 0xFF);
- //Initialize PNG Loading
- int imgFlags = IMG_INIT_PNG;
- if(!(IMG_Init(imgFlags) &imgFlags))
- {
- printf("SDL_image could not initialize! SDL_image Error: %s\n",IMG_GetError());
- success = false;
- }
- }
- }
- }
- return success;
- }
- bool loadMedia()
- {
- //Loading success flag
- bool success = true;
- //Load foo texture
- if(!loadFromFile("surfaces/ritsu.png", &gFooTexture))
- {
- printf("Failed to load Foo texture image!\n");
- success = false;
- }
- //Load background texture
- if(!loadFromFile("surfaces/background.png", &gBackgroundTexture))
- {
- printf("Failed to load background texture image!\n");
- success = false;
- }
- return success;
- }
- void end()
- {
- //Free loaded images
- DeallocTexture(&gFooTexture);
- DeallocTexture(&gBackgroundTexture);
- //Destroy window
- SDL_DestroyRenderer(gRenderer);
- SDL_DestroyWindow(gWindow);
- gWindow = NULL;
- gRenderer = NULL;
- //Quit SDL Subsystems
- IMG_Quit();
- SDL_Quit();
- }
- int main(int argc, char *args[])
- {
- //Start up SDL and create window
- if(!init())
- {
- printf("Failed to initialize!\n");
- }
- else
- {
- //Load media
- if(!loadMedia())
- {
- printf("Failed to load media!\n");
- }
- else
- {
- //Main loop flag
- bool quit = false;
- //Event handler
- SDL_Event e;
- //While application is running
- while(!quit)
- {
- //Handle events on queue
- while(SDL_PollEvent(&e) != 0)
- {
- //User requests quit
- if(e.type == SDL_QUIT)
- {
- quit = true;
- }
- }
- //Clear screen
- SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
- SDL_RenderClear(gRenderer);
- //Render background texture to screen
- render(0, 0, &gBackgroundTexture);
- //Render foo to the screen
- render(240, 190, &gFooTexture);
- //Update screen
- SDL_RenderPresent(gRenderer);
- }
- }
- }
- //Free resources and close SDL
- end();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement