SDL_Texture* renderText(char* message, TTF_Font* font, SDL_Color color, SDL_Renderer *renderer) { //Open the font /*TTF_Font *font = TTF_OpenFont(fontFile.c_str(), fontSize); if (font == nullptr){ logSDLError(std::cout, "TTF_OpenFont"); return nullptr; } */ //We need to first render to a surface as that's what TTF_RenderText //returns, then load that surface into a texture SDL_Surface *surf = TTF_RenderText_Blended(font, message, color); if (surf == NULL){ TTF_CloseFont(font); logSDLError("TTF_RenderText"); return NULL; } SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surf); if (texture == NULL){ logSDLError("CreateTexture"); } //Clean up the surface and font SDL_FreeSurface(surf); //TTF_CloseFont(font); return texture; }
SDL_Texture* LoadTextureFromFile(const String &file, SDL_Renderer *renderer) { SDL_Texture *texture = nullptr; SDL_Surface *loadedImage = nullptr; //android #if defined(__ANDROID__) SDL_RWops *f = SDL_RWFromFile(file.c_str(), "rb"); loadedImage = IMG_Load_RW(f , 1); if(f > 0) __android_log_write(ANDROID_LOG_INFO, "Chain Drop", "File Loaded"); #elif defined(_WIN32) loadedImage = IMG_Load(file.c_str()); #endif //If the loading went ok, convert to texture and return the texture if (loadedImage != nullptr) { texture = SDL_CreateTextureFromSurface(renderer, loadedImage); SDL_FreeSurface(loadedImage); //Make sure converting went ok too if (texture == nullptr) logSDLError(std::cout, "LoadTextureFromFile"); } else logSDLError(std::cout, "LoadTextureFromFile"); return texture; }
int main( int argc, char* args[] ) { SDL_Window* window = NULL; if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { logSDLError(std::cout, "SDL_Init"); return 1; } // window = SDL_CreateWindow( "SDL Game", 100, 100, 640, 480, SDL_WINDOW_SHOWN ); window = SDL_CreateWindow( "SDL Game", 100, 100, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN ); if( window == nullptr ) { logSDLError(std::cout, "CreateWindow"); SDL_Quit(); return 1; } SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 2 ); SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 1 ); // SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE ); SDL_GLContext glcontext = SDL_GL_CreateContext(window); int kbsize; const Uint8 * kb = SDL_GetKeyboardState(&kbsize); std::cerr << "pregame" << std::endl; Game game; std::cerr << "preinput" << std::endl; // game.addInput((void*)kb, kbsize, sizeof(Uint8)); bool quit = false; SDL_Event event; while( !quit ) { while( SDL_PollEvent( &event ) != 0 ) { switch( event.type ) { case SDL_QUIT: quit = true; break; } } // std::cerr << " Loop "; game.Loop(); SDL_GL_SwapWindow(window); // SDL_Delay( 50 ); } cleanup(&glcontext,window); SDL_Quit(); return 0; }
bool Graphic_Util::init_sdl() { // Initialisation de la librairie SDL if (SDL_Init(SDL_INIT_EVERYTHING) != 0 ) { logSDLError(cout, "SDL_Init"); return false; } //Init OpenGL windows window = SDL_CreateWindow("Photobooth",SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, g_config.screen_width, g_config.screen_height, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL); check(); if( window ) { glContext = SDL_GL_CreateContext(window); check(); glEnable(GL_BLEND); check(); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); check(); SDL_GL_SetSwapInterval(1); //This makes our buffer swap syncronized with the monitor's vertical refresh check(); SDL_GetWindowSize(window, &w_width, &w_height); cout << "Windows size : " << w_width << ":" << w_height << endl; } else { logSDLError(cout, "Erreur de création de la fenêtre"); return false; } //Init SDL_Image int flags = IMG_INIT_JPG|IMG_INIT_PNG; int initted = IMG_Init(flags); if((initted & flags) != flags) { cerr << "IMG_Init: Failed to init required jpg and png support!\n" << "IMG_Init: " << IMG_GetError() << endl; return false; } SDL_ShowCursor(0); //Initialize SDL_mixer if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 ) { return false; } return true; }
SDL_Texture *loadBmp(char *loc, SDL_Renderer *ren) { SDL_Texture *tex = NULL; SDL_Surface *loadedImg = SDL_LoadBMP(loc); if (loadedImg != NULL) { tex = SDL_CreateTextureFromSurface(ren, loadedImg); SDL_FreeSurface(loadedImg); if (tex == NULL) logSDLError("CreateTextureFromSurface"); } else { logSDLError("LoadBMP"); } return tex; }
SDL_Texture *loadHeader(char **loc, SDL_Renderer *ren) { SDL_Texture *tex = NULL; SDL_Surface *loadedSource = IMG_ReadXPMFromArray(loc); if (loadedSource != NULL) { tex = SDL_CreateTextureFromSurface(ren, loadedSource); SDL_FreeSurface(loadedSource); if (tex == NULL) logSDLError("CreateTextureFromSource"); } else { logSDLError("LoadSRC"); } return tex; }
int Saloon::init(const char* title, int width, int height, bool windowed, int x, int y) { if (SDL_Init(SDL_INIT_EVERYTHING) != 0){ logSDLError(std::cout, "SDL_Init"); return 1; } SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // Slight optimization to prepare the PNG loader prior to loading any images if ((IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) != IMG_INIT_PNG){ logSDLError(std::cout, "IMG_Init"); return 1; } if (TTF_Init() != 0){ logSDLError(std::cout, "TTF_Init"); return 1; } if(windowed) { _window = SDL_CreateWindow(title, x, y, width, height, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); } else { _window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL); } if (_window == nullptr){ logSDLError(std::cout, "CreateWindow"); return 2; } _displayWidth = width; _displayHeight = height; _halfDisplayWidth = width/2.0f; _halfDisplayHeight = height/2.0f; return 0; }
SDL_Texture* loadTexture(const char* file, SDL_Renderer *ren) { SDL_Texture *texture = IMG_LoadTexture(ren, file); if (texture == NULL) logSDLError("LoadTexture"); return texture; }
SDL_Renderer* InitSDLRenderer(SDL_Window* window) { //SDL_Renderer* renderer = SDL_CreateRenderer(window, -1,SDL_RENDERER_ACCELERATED); SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0); if(renderer != nullptr) { SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"); #ifdef __ANDROID__ SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best"); #elif _WIN32 SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best"); #endif //SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); SDL_RenderSetLogicalSize(renderer, 640, 360); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); } if (renderer == nullptr) { logSDLError(std::cout, "InitSDLRenderer"); return nullptr; } return renderer; }
int init_SDL_all() { SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2 //init image loader if ((IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) != IMG_INIT_PNG){ logSDLError("IMG_Init"); return 1; } if (TTF_Init() != 0){ logSDLError("TTF_Init"); return 1; } return 0; }
/* * Loads an image into a texture on the rendering device * @param file The image file to load * @param ren The renderer to load the texture onto * @return the loaded texture, or nullptr if something went wrong. */ 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; }
SDL_Surface * Font::toSurface(std::string text, Color color) { SDL_Surface *surf = TTF_RenderText_Blended(font_, text.c_str(), color.getSDLColor()); if (surf == 0) { logSDLError(std::cout, "TTF_RenderText"); return 0; } return surf; }
/*{{{ loadTexture: Loads file, returns a texture*/ SDL_Texture *loadTexture(const std::string &file, SDL_Renderer *ren) { SDL_Texture *texture = nullptr; SDL_Surface *loadedImage = IMG_Load(file.c_str()); if(loadedImage != nullptr) { //transparency ffs! (Foer fasen!) SDL_SetColorKey(loadedImage,true,SDL_MapRGB(loadedImage->format,255,0,255)); texture = SDL_CreateTextureFromSurface(ren, loadedImage); SDL_FreeSurface(loadedImage); if(texture == nullptr) logSDLError(std::cerr, "CreateTextureFromSurface"); } else logSDLError(std::cerr, "LoadBMP"); return texture; }
SDL_Surface* LoadSurfaceFromFile(const String &file) { SDL_Surface *surface = IMG_Load(file.c_str()); if (surface == nullptr) logSDLError(std::cout, "LoadSurfaceFromFile"); return surface; }
/* * Loads a BMP image into a texture on the rendering device * @param file The BMP image file to load * @param ren The renderer to load the texture onto * @return the loaded texture, or nullptr if something went wrong. */ SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren){ SDL_Texture *texture = nullptr; //Load the image SDL_Surface *loadedImage = SDL_LoadBMP(file.c_str()); //If the loading went ok, convert to texture and return the texture if (loadedImage != nullptr){ texture = SDL_CreateTextureFromSurface(ren, loadedImage); SDL_FreeSurface(loadedImage); //Make sure converting went ok too if (texture == nullptr){ logSDLError(std::cout, "CreateTextureFromSurface"); } } else { logSDLError(std::cout, "LoadBMP"); } return texture; }
Font::Font(std::string font, int size) { ttf_file_ = font; this->size_ = size; this->font_ = TTF_OpenFont(ttf_file_.c_str(), size); if (this->font_ == 0) { logSDLError(std::cout, "TTF_OpenFont"); } scale_ = 1.0; }
SDL_Texture* Game::loadTexture(const char* file, SDL_Renderer* ren) { SDL_Texture* tex = IMG_LoadTexture(ren, file); if (tex == nullptr) { logSDLError(std::cout, "LoadTexture"); } return tex; }
void TextLine::loadText(std::string message) { //We need to first render to a surface as that's what TTF_RenderText //returns, then load that surface into a texture TTF_Font * font = mTextMaker->getFont(); SDL_Renderer * renderer = mTextMaker->mRenderer; SDL_Surface * surf = TTF_RenderText_Blended(font, mText.c_str(), mColour); if (surf == nullptr){ logSDLError(std::cout, "TTF_LoadTextLine"); mTexture = nullptr; } SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, surf); if (texture == nullptr){ logSDLError(std::cout, "CreateTexture"); } //Clean up the surface and font SDL_FreeSurface(surf); mTexture = texture; }
Font::Font() { ttf_file_ = "./resources/DejaVuSans.ttf"; size_ = 20; font_ = TTF_OpenFont(ttf_file_.c_str(), size_); if (font_ == 0) { logSDLError(std::cout, "TTF_OpenFont"); } scale_ = 1.0; }
bool init_drawing(SDL_Renderer* ren) { font = TTF_OpenFont(fontFile, fontSize); if (font == NULL){ logSDLError("TTF_OpenFont"); return false; } fontLarge = TTF_OpenFont(fontFile, fontSizeLarge); if (fontLarge == NULL){ logSDLError("TTF_OpenFont"); return false; } sheet = loadTexture("images/tile_sheet.png", ren); init_animation(); return true; }
void Font::scale(double s) { if (s != scale_) { scale_ = s; TTF_CloseFont(font_); font_ = TTF_OpenFont(ttf_file_.c_str(), scale_ * size_); if (font_ == 0) { logSDLError(std::cout, "TTF_OpenFont"); } } }
/** * Loads a BMP image into a texture on the rendering device * @param file The BMP image file to load * @param ren The renderer to load the texture onto * @return the loaded texture, or nullptr if something went wrong. */ SDL_Texture* CKT::GFX::loadTextureBmp(const std::string &file, SDL_Renderer *ren) { //Initialize to nullptr to avoid dangling pointer issues SDL_Texture *texture = nullptr; //Load the image SDL_Surface *loadedImage = SDL_LoadBMP(file.c_str()); //If the loading went ok, convert to texture and return the texture if (loadedImage != nullptr) { texture = SDL_CreateTextureFromSurface(ren, loadedImage); SDL_FreeSurface(loadedImage); //Make sure converting went ok too if (texture == nullptr) { logSDLError(std::cout, "CreateTextureFromSurface()"); } } else { logSDLError(std::cout, "LoadBMP()"); } return texture; }
SDL_Texture *renderText(const std::string &msg, const std::string &fontPath, SDL_Color color, int fontSize, SDL_Renderer *ren) { TTF_Font *font = TTF_OpenFont(fontPath.c_str(), fontSize); if(font == nullptr) { logSDLError("Unable to open font"); return nullptr; } SDL_Surface *surface = TTF_RenderText_Blended(font, msg.c_str(), color); if(surface == nullptr) { TTF_CloseFont(font); logSDLError("Unable to render text to a surface"); return nullptr; } SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, surface); if(tex == nullptr) { logSDLError("Unable to render surface to texture"); } SDL_FreeSurface(surface); TTF_CloseFont(font); return tex; }
TTF_Font* LoadFont(const String &file, int fontSize) { TTF_Font *font = nullptr; #if defined(__ANDROID__) SDL_RWops *f = SDL_RWFromFile(file.c_str(), "rb"); font = TTF_OpenFontRW(f, 1, fontSize); #elif defined(_WIN32) font = TTF_OpenFont(file.c_str(), fontSize); #endif if (font == nullptr) logSDLError(std::cout, "OpenFont"); return font; }
void Painter::paintText(std::string text, int x, int y) { if (text.length() == 0) return; SDL_Surface * surface = status.font->toSurface(text, status.pen); SDL_Rect r = status.font->textBounds(text); r.x = x; r.y = y; status.transformation->applyTransformation(r.x, r.y); SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface); if (texture == 0) { logSDLError(std::cout, "CreateTexture"); } SDL_RenderCopy(renderer, texture, NULL, &r); SDL_FreeSurface(surface); SDL_DestroyTexture(texture); }
SDL_Window* InitSDL(const char* windowName, int windowWidth, int windowHeight) { SDL_Init(SDL_INIT_EVERYTHING); IMG_Init(IMG_INIT_PNG); SDL_Window *window; #if defined(_WIN32) window = SDL_CreateWindow(windowName, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowWidth, windowHeight, SDL_WINDOW_OPENGL); #elif defined(__ANDROID__) window = SDL_CreateWindow(windowName, 0, 0, windowWidth, windowHeight, SDL_WINDOW_OPENGL); #endif if (window == nullptr) { logSDLError(std::cout, "InitSDL"); return nullptr; } return window; }
SDL_Rect Font::textBounds(std::string text) { if (text.length() == 0) { return SDL_Rect(); } Color c; SDL_Surface *surf = TTF_RenderText_Blended(font_, text.c_str(), c.getSDLColor()); SDL_Rect r; if (surf == 0) { logSDLError(std::cout, "TTF_RenderText"); r.x = 0; r.y = 0; r.w = 0; r.h = 0; return r; } r = surf->clip_rect; SDL_FreeSurface(surf); return r; }
int main(int argc, char** argv) { if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { logSDLError(std::cout, "SDL_INIT"); return __LINE__; } SDL_Window *window = SDL_CreateWindow("Lesson 2", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT) SDL_RenderClear(ren); SDL_RenderCopy(ren, tex, NULL, NULL); SDL_RenderPresent(ren); SDL_Delay(2000); SDL_DestroyTexture(tex); SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); SDL_Quit(); }
SDL_Surface* CreateSurface(int width, int height) { #if SDL_BYTEORDER == SDL_BIG_ENDIAN const int rmask = 0xff000000; const int gmask = 0x00ff0000; const int bmask = 0x0000ff00; const int amask = 0x000000ff; #else const int rmask = 0x000000ff; const int gmask = 0x0000ff00; const int bmask = 0x00ff0000; const int amask = 0xff000000; #endif SDL_Surface* surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, rmask, gmask, bmask, amask); if (surface == nullptr) logSDLError(std::cout, "CreateSurface"); return surface; }
SDL_Texture *loadTexture(char *loc, SDL_Renderer *ren) { SDL_Texture *tex = IMG_LoadTexture(ren, loc); if (tex == NULL) logSDLError("LoadTexture"); return tex; }