Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////
- // main.cpp //
- //////////////
- #include "engine.h"
- /* SOME OTHER STUFF HERE */
- texture *stuffAndThings[] =
- {
- newTexture("texture1.bmp", SCR_W, SCR_H),
- newTexture("up.bmp", SCR_W, SCR_H),
- newTexture("down.bmp", SCR_W, SCR_H),
- newTexture("left.bmp", SCR_W, SCR_H),
- newTexture("right.bmp", SCR_W, SCR_H)
- };
- int main(int numArgs, char **args)
- {
- newTexture("texture1.bmp", SCR_W, SCR_H);
- newTexture("up.bmp", SCR_W, SCR_H);
- newTexture("down.bmp", SCR_W, SCR_H);
- newTexture("left.bmp", SCR_W, SCR_H);
- newTexture("right.bmp", SCR_W, SCR_H);
- if(init() && initMedia()) SDL_Delay(5000);
- quit();
- printf("\n\nThe program has ended..\n"); fflush(stdin); getchar();
- return 0;
- }
- /////////////////
- // texture.cpp //
- /////////////////
- #include "engine.h"
- SDL_Texture *texError = NULL;
- std::vector<texture*> textures;
- texture::texture(const char *_path, int w, int h) : path(_path), t(NULL)
- {
- r.w = w;
- r.h = h ? h : w;
- r.x = r.y = 0;
- }
- texture::~texture() { if(t != texError) SDL_DestroyTexture(t); }
- void texture::draw()
- {
- if(!t) return;
- SDL_RenderCopy(renderer, t, NULL, &r);
- }
- SDL_Texture *openImage(const char *path)
- {
- if(!path) return NULL;
- log("Texture: %s\n", path);
- SDL_Surface *s = IMG_Load(path);
- if(!s)
- {
- log("Failed to open image file: %s\n", IMG_GetError());
- return NULL;
- }
- SDL_Texture *t = SDL_CreateTextureFromSurface(renderer, s);
- SDL_FreeSurface(s);
- if(!t)
- {
- log("SDL_CreateTextureFromSurface failed: %s\n", SDL_GetError());
- t = NULL;
- }
- return t;
- }
- texture *newTexture(const char *path, int w, int h)
- {
- texture *_newTexture = new texture(path, w, h);
- for(size_t i = 0; i < textures.size(); ++i)
- if(!textures[i])
- {
- textures[i] = _newTexture;
- return _newTexture;
- }
- textures.push_back(_newTexture);
- return _newTexture;
- }
- bool loadTextures()
- {
- if(!(texError = openImage("texerror.bmp"))) return false;
- for(size_t i = 0; i < textures.size(); ++i) textures[i]->t = openImage(textures[i]->path);
- return true;
- }
- void freeTextures()
- {
- for(size_t i = 0; i < textures.size(); ++i)
- {
- delete textures[i];
- textures[i] = NULL;
- }
- SDL_DestroyTexture(texError);
- texError = NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment