Skip to content

Instantly share code, notes, and snippets.

@swarminglogic
Last active December 18, 2015 22:59
Show Gist options
  • Save swarminglogic/5858646 to your computer and use it in GitHub Desktop.
Save swarminglogic/5858646 to your computer and use it in GitHub Desktop.
SFML-vs-SDL-benchmark, forked from github.com/eXpl0it3r/SFML-vs-SDL-Benchmark. Since it's just a silly and fairly nonconstructive benchmark, I prefer to not have it listed as a forked repository.

SFML-vs-SDL-Benchmark

SwarmingLogic updated SDL from 1.2 to SDL 2.0.0, and adding SCons build [May 2013]

Adapted by eXpl0it3r for SFML 2 [Aug 2012].

A small SFML vs SDL benchmark written by Laurent Gomila [2007]

#!/bin/bash
mkdir src
mv Main.cpp SConscript Sprites-alpha.cpp Sprites.cpp Sprites-rotating.cpp Text-dynamic.cpp Text-static.cpp src/
mkdir data
mv cheeseburger.ttf image1.bmp image2.bmp image3.bmp image4.bmp image5.bmp data
#include <iomanip>
#include <iostream>
int spritesSDL();
int spritesSFML();
int spritesAlphaSDL();
int spritesAlphaSFML();
int spritesRotatingSDL();
int spritesRotatingSFML();
int textStaticSDL();
int textStaticSFML();
int textDynamicSDL();
int textDynamicSFML();
void Test(int (*sdlFunc)(), int (*sfmlFunc)())
{
int SDL = sdlFunc();
int SFML = sfmlFunc();
if (!SDL || !SFML) {
std::cout << "Failed when running tests." << std::endl;
exit(EXIT_FAILURE);
}
float factor = static_cast<float>(SDL) / SFML;
float factorInv = static_cast<float>(SFML) / SDL;
std::cout << std::fixed << std::setprecision(2);
std::cout << "SDL displayed " << SDL << " frames" << std::endl;
std::cout << "SFML displayed " << SFML << " frames" << std::endl;
if (factor >= 1.0f)
std::cout << "--> SDL is " << factor << "x as fast as SFML" << std::endl;
else
std::cout << "--> SFML is " << factorInv << "x as fast as SDL" << std::endl;
std::cout << std::endl;
}
int main(int argc, char *argv[])
{
std::cout << "1/ Test : sprites" << std::endl;
Test(&spritesSDL, &spritesSFML);
std::cout << "2/ Test : alpha-blended sprites" << std::endl;
Test(&spritesAlphaSDL, &spritesAlphaSFML);
std::cout << "3/ Test : rotating sprites" << std::endl;
Test(&spritesRotatingSDL, &spritesRotatingSFML);
std::cout << "4/ Test : static text" << std::endl;
Test(&textStaticSDL, &textStaticSFML);
std::cout << "5/ Test : dynamic text" << std::endl;
Test(&textDynamicSDL, &textDynamicSFML);
std::cout << "Press enter to exit..." << std::endl;
std::cin.ignore(10000, '\n');
return EXIT_SUCCESS;
}
import os, re, imp
Import('env')
base ='#/src'
target = '#/bin/main'
cppFiles = Glob('*.cpp') + Glob('*/*.cpp') + Glob('*/*/*.cpp')
cppFilesPrune = Glob('Main.cpp') + Glob('Test*.cpp') + Glob('*/Test*.cpp') + Glob('*/*/Test*.cpp')
testFiles = Glob('Test*.h') + Glob('*/Test*.h') + Glob('*/*/Test*.h')
sourceFiles = list(set(cppFiles) - set(cppFilesPrune))
pathSdl = os.environ["SDL2DIR"];
pathSFML = os.environ["SFML2DIR"];
libs = [
'SDL2', 'SDL2_image', 'SDL2_ttf', 'SDL2_mixer',
'sfml-system', 'sfml-graphics', 'sfml-audio', 'sfml-window',
'pthread', 'GL', 'GLU', 'm', 'dl', 'rt']
libpaths = [
pathSdl + "/lib",
pathSFML + "/lib"]
sourcepaths = [
base,
pathSdl + "/include/SDL2",
pathSFML + "/include/"]
cppflags = ['-O3']
# Adding ignore paths for external libraries
cppflags.append(['-isystem', pathSdl + '/include/'])
if env['CXX'] == 'g++':
cppflags.append(['-std=c++0x'])
# cppflags.append([
# '-Wall', '-Wextra', '-Wcast-align', '-Wcast-qual',
# '-Wconversion', '-Wdisabled-optimization', '-Weffc++',
# '-Wfloat-equal', '-Wformat=2', '-Wimport', '-Winit-self',
# '-Winline', '-Winvalid-pch', '-Wlong-long',
# '-Wmissing-format-attribute', '-Wmissing-include-dirs',
# '-Wmissing-noreturn', '-Wpacked', '-Wpointer-arith',
# '-Wredundant-decls', '-Wshadow', '-Wstack-protector',
# '-Wstrict-aliasing=2',
# '-Wunsafe-loop-optimizations', '-Wunused',
# '-Wvariadic-macros', '-Wwrite-strings', '-pedantic',
# '-pedantic-errors', '-Woverloaded-virtual',
# '-Wswitch-enum'])
elif env['CXX'] == 'clang':
cppflags.append(['-std=c++11'])
applib = env.StaticLibrary('#/lib/main', sourceFiles,
LIBS = libs,
LIBPATH = libpaths,
CPPFLAGS = cppflags,
CPPPATH = sourcepaths);
Default(applib)
Clean(applib, '#/build')
cppMain = Glob('Main.cpp')
app = env.Program(target, cppMain,
LIBS = applib + libs,
LIBPATH = libpaths,
CPPFLAGS = cppflags,
CPPPATH = sourcepaths);
Default(app)
Clean(app, '#/build')
import os
env = Environment(ENV = {'PATH' : os.environ['PATH'],
'HOME' : os.environ['HOME'],
'TERM' : 'xterm'},
# CXX='clang',
CXX='g++',
tools=['default'], toolpath=['../common'])
VariantDir('build', 'src')
Export('env')
SConscript('build/SConscript')
#include <ctime>
#include <iostream>
#include <SDL.h>
#include <SFML/Graphics.hpp>
const int nbSprites = 2000;
int spritesAlphaSDL()
{
// Initialize the random numbers generator
srand(static_cast<unsigned int>(time(NULL)));
// Initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
std::cout << SDL_GetError() << std::endl;
return EXIT_FAILURE;
}
// // Create the main window
SDL_Window* win = SDL_CreateWindow("SDL - SpritesAlpha",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
800, 600, SDL_WINDOW_SHOWN);
if (!win) {
std::cout << SDL_GetError() << std::endl;
return EXIT_FAILURE;
}
SDL_Renderer* renderer =
SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
std::cout << SDL_GetError() << std::endl;
return EXIT_FAILURE;
}
// Load five images
const int N_IMAGES = 5;
SDL_Surface* surfaces[N_IMAGES];
surfaces[0] = SDL_LoadBMP("data/image1.bmp");
surfaces[1] = SDL_LoadBMP("data/image2.bmp");
surfaces[2] = SDL_LoadBMP("data/image3.bmp");
surfaces[3] = SDL_LoadBMP("data/image4.bmp");
surfaces[4] = SDL_LoadBMP("data/image5.bmp");
if (!surfaces[0] || !surfaces[1] || !surfaces[2] ||
!surfaces[3] || !surfaces[4]) {
return EXIT_FAILURE;
}
// Create HW accelerated textures from surfaces
SDL_Texture* textures[N_IMAGES];
for (size_t i = 0 ; i < N_IMAGES ; ++i) {
textures[i] = SDL_CreateTextureFromSurface(renderer, surfaces[i]);
}
for (int i = 0; i < N_IMAGES; ++i) {
SDL_SetTextureBlendMode(textures[i], SDL_BLENDMODE_BLEND);
SDL_SetTextureAlphaMod(textures[i], 100);
}
// Create a lot of sprites
struct Sprite
{
SDL_Rect rect;
SDL_Texture* tex;
};
Sprite sprites[nbSprites];
for (int i = 0; i < nbSprites; ++i) {
const int imageIndex = rand() % 5;
sprites[i].tex = textures[imageIndex];
sprites[i].rect.x = rand() % 700;
sprites[i].rect.y = rand() % 500;
sprites[i].rect.w = surfaces[imageIndex]->w;
sprites[i].rect.h = surfaces[imageIndex]->h;
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
// Start the timer
Uint32 time = SDL_GetTicks();
int nbFrames = 0;
// Start game loop
while (SDL_GetTicks() < time + 5000) {
// Clear the screen
SDL_RenderClear(renderer);
// Draw the sprites
for (int i = 0; i < nbSprites; ++i) {
SDL_RenderCopy(renderer, sprites[i].tex, NULL, &sprites[i].rect);
}
// Finally, display rendered frame on screen
SDL_RenderPresent(renderer);
++nbFrames;
}
////////////////////////////////////////
// Clean up
for (size_t i = 0 ; i < N_IMAGES ; ++i) {
SDL_FreeSurface(surfaces[i]);
SDL_DestroyTexture(textures[i]);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
// Exit SDL
SDL_Quit();
return nbFrames;
}
int spritesAlphaSFML()
{
// Create the main window
sf::RenderWindow app(sf::VideoMode(800, 600), "SFML - SpritesAlpha");
// Load five images
sf::Texture textures[5];
if (!textures[0].loadFromFile("data/image1.bmp") ||
!textures[1].loadFromFile("data/image2.bmp") ||
!textures[2].loadFromFile("data/image3.bmp") ||
!textures[3].loadFromFile("data/image4.bmp") ||
!textures[4].loadFromFile("data/image5.bmp")) {
return 1;
}
// Create a lot of sprites
sf::Sprite sprites[nbSprites];
for (int i = 0; i < nbSprites; ++i) {
sprites[i].setTexture(textures[rand() % 5]);
sprites[i].setPosition(rand() % 700, rand() % 500);
sprites[i].setColor(sf::Color(255, 255, 255, 100));
}
// Start the timer
sf::Clock clock;
int nbFrames = 0;
// Start game loop
while (clock.getElapsedTime().asSeconds() < 5.f) {
// Clear the screen
app.clear();
// Draw the sprites
for (int i = 0; i < nbSprites; ++i)
app.draw(sprites[i]);
// Finally, display rendered frame on screen
app.display();
nbFrames++;
}
return nbFrames;
}
#include <ctime>
#include <iostream>
#include <SDL.h>
#include <SFML/Graphics.hpp>
const int nbSprites = 2000;
int spritesRotatingSDL()
{
// Initialize the random numbers generator
srand(static_cast<unsigned int>(time(NULL)));
// Initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
std::cout << SDL_GetError() << std::endl;
return EXIT_FAILURE;
}
// // Create the main window
SDL_Window* win = SDL_CreateWindow("SDL - SpritesRotating",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
800, 600, SDL_WINDOW_SHOWN);
if (!win) {
std::cout << SDL_GetError() << std::endl;
return EXIT_FAILURE;
}
SDL_Renderer* renderer =
SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
std::cout << SDL_GetError() << std::endl;
return EXIT_FAILURE;
}
// Load five images
const int N_IMAGES = 5;
SDL_Surface* surfaces[N_IMAGES];
surfaces[0] = SDL_LoadBMP("data/image1.bmp");
surfaces[1] = SDL_LoadBMP("data/image2.bmp");
surfaces[2] = SDL_LoadBMP("data/image3.bmp");
surfaces[3] = SDL_LoadBMP("data/image4.bmp");
surfaces[4] = SDL_LoadBMP("data/image5.bmp");
if (!surfaces[0] || !surfaces[1] || !surfaces[2] ||
!surfaces[3] || !surfaces[4]) {
return EXIT_FAILURE;
}
// Create HW accelerated textures from surfaces
SDL_Texture* textures[N_IMAGES];
for (size_t i = 0 ; i < N_IMAGES ; ++i) {
textures[i] = SDL_CreateTextureFromSurface(renderer, surfaces[i]);
}
// Create a lot of sprites
struct Sprite
{
SDL_Rect rect;
SDL_Texture* tex;
};
Sprite sprites[nbSprites];
for (int i = 0; i < nbSprites; ++i) {
const int imageIndex = rand() % 5;
sprites[i].tex = textures[imageIndex];
sprites[i].rect.x = rand() % 700;
sprites[i].rect.y = rand() % 500;
sprites[i].rect.w = surfaces[imageIndex]->w;
sprites[i].rect.h = surfaces[imageIndex]->h;
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
// Start the timer
Uint32 time = SDL_GetTicks();
int nbFrames = 0;
// Start game loop
while (SDL_GetTicks() < time + 5000) {
// Clear the screen
SDL_RenderClear(renderer);
// Draw the sprites
float angle = (SDL_GetTicks() - time) / 10.0f;
for (int i = 0; i < nbSprites; ++i) {
SDL_RenderCopyEx(renderer, sprites[i].tex, NULL, &sprites[i].rect,
angle, NULL, SDL_FLIP_NONE);
}
// Finally, display rendered frame on screen
SDL_RenderPresent(renderer);
++nbFrames;
}
////////////////////////////////////////
// Clean up
for (size_t i = 0 ; i < N_IMAGES ; ++i) {
SDL_FreeSurface(surfaces[i]);
SDL_DestroyTexture(textures[i]);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
// Exit SDL
SDL_Quit();
return nbFrames;
}
int spritesRotatingSFML()
{
// Create the main window
sf::RenderWindow app(sf::VideoMode(800, 600), "SFML - SpritesRotating");
// Load five images
sf::Texture textures[5];
if (!textures[0].loadFromFile("data/image1.bmp") ||
!textures[1].loadFromFile("data/image2.bmp") ||
!textures[2].loadFromFile("data/image3.bmp") ||
!textures[3].loadFromFile("data/image4.bmp") ||
!textures[4].loadFromFile("data/image5.bmp")) {
return 1;
}
// Create a lot of sprites
sf::Sprite sprites[nbSprites];
for (int i = 0; i < nbSprites; ++i) {
sprites[i].setTexture(textures[rand() % 5]);
sprites[i].setPosition(rand() % 700, rand() % 500);
sprites[i].setOrigin(sprites[i].getLocalBounds().width /
2.f, sprites[i].getLocalBounds().height / 2.f);
}
// Start the timer
sf::Clock clock;
int nbFrames = 0;
// Start game loop
while (clock.getElapsedTime().asSeconds() < 5.f) {
// Clear the screen
app.clear();
// Draw the sprites
float Angle = clock.getElapsedTime().asSeconds() * 100.f;
for (int i = 0; i < nbSprites; ++i) {
sprites[i].setRotation(Angle);
app.draw(sprites[i]);
}
// Finally, display rendered frame on screen
app.display();
nbFrames++;
}
return nbFrames;
}
#include <ctime>
#include <iostream>
#include <SDL.h>
#include <SFML/Graphics.hpp>
const int nbSprites = 2000;
int spritesSDL()
{
// Initialize the random numbers generator
srand(static_cast<unsigned int>(time(NULL)));
// Initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
std::cout << SDL_GetError() << std::endl;
return EXIT_FAILURE;
}
// // Create the main window
SDL_Window* win = SDL_CreateWindow("SDL - Sprites",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
800, 600, SDL_WINDOW_SHOWN);
if (!win) {
std::cout << SDL_GetError() << std::endl;
return EXIT_FAILURE;
}
SDL_Renderer* renderer =
SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
std::cout << SDL_GetError() << std::endl;
return EXIT_FAILURE;
}
// Load five images
const int N_IMAGES = 5;
SDL_Surface* surfaces[N_IMAGES];
surfaces[0] = SDL_LoadBMP("data/image1.bmp");
surfaces[1] = SDL_LoadBMP("data/image2.bmp");
surfaces[2] = SDL_LoadBMP("data/image3.bmp");
surfaces[3] = SDL_LoadBMP("data/image4.bmp");
surfaces[4] = SDL_LoadBMP("data/image5.bmp");
if (!surfaces[0] || !surfaces[1] || !surfaces[2] ||
!surfaces[3] || !surfaces[4]) {
return EXIT_FAILURE;
}
// Create HW accelerated textures from surfaces
SDL_Texture* textures[N_IMAGES];
for (size_t i = 0 ; i < N_IMAGES ; ++i) {
textures[i] = SDL_CreateTextureFromSurface(renderer, surfaces[i]);
}
// Create a lot of sprites
struct Sprite
{
SDL_Rect rect;
SDL_Texture* tex;
};
Sprite sprites[nbSprites];
for (int i = 0; i < nbSprites; ++i) {
const int imageIndex = rand() % 5;
sprites[i].tex = textures[imageIndex];
sprites[i].rect.x = rand() % 700;
sprites[i].rect.y = rand() % 500;
sprites[i].rect.w = surfaces[imageIndex]->w;
sprites[i].rect.h = surfaces[imageIndex]->h;
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
// Start the timer
Uint32 time = SDL_GetTicks();
int nbFrames = 0;
// Start game loop
while (SDL_GetTicks() < time + 5000) {
// Clear the screen
SDL_RenderClear(renderer);
for (int i = 0; i < nbSprites; ++i) {
SDL_RenderCopy(renderer, sprites[i].tex, NULL, &sprites[i].rect);
}
SDL_RenderPresent(renderer);
++nbFrames;
}
////////////////////////////////////////
// Clean up
for (size_t i = 0 ; i < N_IMAGES ; ++i) {
SDL_FreeSurface(surfaces[i]);
SDL_DestroyTexture(textures[i]);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
// Exit SDL
SDL_Quit();
return nbFrames;
}
int spritesSFML()
{
// Create the main window
sf::RenderWindow app(sf::VideoMode(800, 600), "SFML - Sprites");
// Load five images
sf::Texture textures[5];
if (!textures[0].loadFromFile("data/image1.bmp") ||
!textures[1].loadFromFile("data/image2.bmp") ||
!textures[2].loadFromFile("data/image3.bmp") ||
!textures[3].loadFromFile("data/image4.bmp") ||
!textures[4].loadFromFile("data/image5.bmp")) {
return 1;
}
// Create a lot of sprites
sf::Sprite sprites[nbSprites];
for (int i = 0; i < nbSprites; ++i)
{
sprites[i].setTexture(textures[rand() % 5]);
sprites[i].setPosition(rand() % 700, rand() % 500);
}
// Start the timer
sf::Clock clock;
int nbFrames = 0;
// Start game loop
while (clock.getElapsedTime().asSeconds() < 5.f)
{
// Clear the screen
app.clear();
// Draw the sprites
for (int i = 0; i < nbSprites; ++i)
app.draw(sprites[i]);
// Finally, display rendered frame on screen
app.display();
nbFrames++;
}
return nbFrames;
}
#include <iomanip>
#include <iostream>
#include <sstream>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SFML/Graphics.hpp>
const int nbLines = 20;
int textDynamicSDL()
{
// Initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
std::cout << SDL_GetError() << std::endl;
return EXIT_FAILURE;
}
// // Create the main window
SDL_Window* win = SDL_CreateWindow("SDL - TextDynamic",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
800, 600, SDL_WINDOW_SHOWN);
if (!win) {
std::cout << SDL_GetError() << std::endl;
return EXIT_FAILURE;
}
SDL_Renderer* renderer =
SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
std::cout << SDL_GetError() << std::endl;
return EXIT_FAILURE;
}
if ( TTF_Init() != 0) {
std::cerr << "Error with TTF_Init(): " << TTF_GetError() << std::endl;
return EXIT_FAILURE;
}
TTF_Font* font = TTF_OpenFont("data/cheeseburger.ttf", 30);
if (!font)
return 1;
// Start the timer
Uint32 time = SDL_GetTicks();
int nbFrames = 0;
// Start game loop
while (SDL_GetTicks() < time + 5000) {
// Clear the screen
SDL_RenderClear(renderer);
// Draw the text
for (int i = 0; i < nbLines; ++i) {
// Use different styles
switch (i % 5) {
case 0 : TTF_SetFontStyle(font, TTF_STYLE_NORMAL); break;
case 1 : TTF_SetFontStyle(font, TTF_STYLE_BOLD); break;
case 2 : TTF_SetFontStyle(font, TTF_STYLE_ITALIC); break;
case 3 : TTF_SetFontStyle(font, TTF_STYLE_UNDERLINE); break;
case 4 : TTF_SetFontStyle(font, (TTF_STYLE_BOLD |
TTF_STYLE_ITALIC |
TTF_STYLE_UNDERLINE)); break;
}
// Define the dynamic text
std::ostringstream oss;
oss << "Hi, I'm the line number " << i << " and the time elapsed is "
<< std::setprecision(2) << ((SDL_GetTicks() - time) / 1000.f)
<< " sec";
// Render the text to a surface
SDL_Color textColor = {0, 128, 128};
SDL_Surface* surfaceText =
TTF_RenderText_Blended(font, oss.str().c_str(), textColor);
SDL_Texture* text =
SDL_CreateTextureFromSurface(renderer, surfaceText);
// Define the text rectangle
SDL_Rect rect;
rect.x = 0;
rect.y = i * 30;
rect.w = surfaceText->w;
rect.h = surfaceText->h;
// Blit the text surface
SDL_RenderCopy(renderer, text, NULL, &rect);
// Free the surface
SDL_FreeSurface(surfaceText);
SDL_DestroyTexture(text);
}
// Finally, display rendered frame on screen
SDL_RenderPresent(renderer);
++nbFrames;
}
////////////////////////////////////////
// Clean up
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
return nbFrames;
}
int textDynamicSFML()
{
// Create the main window
sf::RenderWindow app(sf::VideoMode(800, 600), "SFML - TextDynamic");
// Load the font
sf::Font textFont;
if (!textFont.loadFromFile("data/cheeseburger.ttf"))
return 1;
// Create the string
const char* textString = "AZERTYUIOPQSDFGHJKLMWXCVBN"
"azertyuiopqsdfghjklmwxcvbn0123456789";
sf::Text text(textString, textFont, 30.f);
text.setColor(sf::Color(0, 128, 128));
// Start the timer
sf::Clock clock;
int nbFrames = 0;
// Start game loop
while (clock.getElapsedTime().asSeconds() < 5.f) {
// Clear the screen
app.clear();
// Draw the text
for (int i = 0; i < nbLines; ++i) {
// Use different styles
switch (i % 5) {
case 0 : text.setStyle(sf::Text::Regular); break;
case 1 : text.setStyle(sf::Text::Bold); break;
case 2 : text.setStyle(sf::Text::Italic); break;
case 3 : text.setStyle(sf::Text::Underlined); break;
case 4 : text.setStyle(sf::Text::Bold |
sf::Text::Italic |
sf::Text::Underlined); break;
}
// Define the dynamic text
std::ostringstream oss;
oss << "Hi, I'm the line number " << i << " and the time elapsed is "
<< std::setprecision(2) << clock.getElapsedTime().asSeconds()
<< " sec";
text.setString(oss.str());
// Draw the string
text.setPosition(text.getPosition().x, i * 30.f);
app.draw(text);
}
// Finally, display rendered frame on screen
app.display();
nbFrames++;
}
return nbFrames;
}
#include <iostream>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SFML/Graphics.hpp>
const int nbLines = 20;
int textStaticSDL()
{
// Initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
std::cout << SDL_GetError() << std::endl;
return EXIT_FAILURE;
}
// // Create the main window
SDL_Window* win = SDL_CreateWindow("SDL - TextStatic",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
800, 600, SDL_WINDOW_SHOWN);
if (!win) {
std::cout << SDL_GetError() << std::endl;
return EXIT_FAILURE;
}
SDL_Renderer* renderer =
SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
std::cout << SDL_GetError() << std::endl;
return EXIT_FAILURE;
}
if ( TTF_Init() != 0) {
std::cerr << "Error with TTF_Init(): " << TTF_GetError() << std::endl;
return EXIT_FAILURE;
}
TTF_Font* font = TTF_OpenFont("data/cheeseburger.ttf", 30);
if (!font)
return 1;
// Render the text to a surface
const char* textString = "AZERTYUIOPQSDFGHJKLMWXCVBN"
"azertyuiopqsdfghjklmwxcvbn0123456789";
SDL_Color textColor = {0, 128, 128};
SDL_Surface* textSurface = TTF_RenderText_Blended(font, textString, textColor);
if (!textSurface)
return 1;
SDL_Texture* text = SDL_CreateTextureFromSurface(renderer, textSurface);
if (!text)
return 1;
// Define the text rectangle
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = textSurface->w;
rect.h = textSurface->h;
// Start the timer
Uint32 time = SDL_GetTicks();
int nbFrames = 0;
// Start game loop
while (SDL_GetTicks() < time + 5000) {
// Clear the screen
SDL_RenderClear(renderer);
// Draw the text
for (int i = 0; i < nbLines; ++i) {
rect.y = i * 30;
SDL_RenderCopy(renderer, text, NULL, &rect);
}
// Finally, display rendered frame on screen
SDL_RenderPresent(renderer);
++nbFrames;
}
////////////////////////////////////////
// Clean up
SDL_FreeSurface(textSurface);
SDL_DestroyTexture(text);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
return nbFrames;
}
int textStaticSFML()
{
// Create the main window
sf::RenderWindow app(sf::VideoMode(800, 600), "SDL - TextStatic");
// Load the font
sf::Font textFont;
if (!textFont.loadFromFile("data/cheeseburger.ttf"))
return 1;
// Create the string
const char* textstring = "AZERTYUIOPQSDFGHJKLMWXCVBN"
"azertyuiopqsdfghjklmwxcvbn0123456789";
sf::Text text(textstring, textFont, 30.f);
text.setColor(sf::Color(0, 128, 128));
// Start the timer
sf::Clock clock;
int nbFrames = 0;
// Start game loop
while (clock.getElapsedTime().asSeconds() < 5.f) {
// Clear the screen
app.clear();
// Draw the text
for (int i = 0; i < nbLines; ++i) {
text.setPosition(text.getPosition().x, i * 30.f);
app.draw(text);
}
// Finally, display rendered frame on screen
app.display();
nbFrames++;
}
return nbFrames;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment