My board game is using a matrix. I created a board using rectangles. gimprimir() takes the matrix and translates it creating different types of rectangles.
I need to insert an image on a part of the screen. This tutorial teaches how to put an image on screen, but not how to load it in a part of the screen. Whenever I try to modify it, I get a segmentation fault. I want to keep it simple. The image format is .bmp.
My code:
bool loadMedia();
void close();
SDL_Texture* loadTexture( std::string path );
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
const int SCREEN_WIDTH = 1200;
const int SCREEN_HEIGHT = 680;
int main(int argc, char* args[]){
nodo a;
if( !init() ){
cout << "Failed to initialize!\n" ;
}
else{
if(!loadMedia()){
cout << "Failed to load media!\n";
}
else{
bool quit = false; // flag ciclo principal
SDL_Event e; // variables manejo de eventos
SDL_Event t;
gimprimir(a);
}
}
}
Function gimprimir() prints a board using struct a:
struct nodo {
array<array<int,19>,19> tablero; // Estado tablero
array<int,2> p1,p2; // Posiciones
int d1,d2; // Distancias mínimas
int paredes1;
int paredes2;
} a;
gimprimir() uses empty and filled rectangles:
void gimprimir(nodo aa){
//Clear screen
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
int i,j;
for (i=1; i<=17; i++){
if (i%2==1){
for (j=1; j<=17 ; j++){
if (j%2==1){
if (aa.p1[0]==i && aa.p1[1]==j){ // FICHA 1
SDL_Rect fillRect = {(50+SCREEN_WIDTH/11)+(SCREEN_WIDTH/11)*(j-1)/2+34, (SCREEN_HEIGHT/11)+(SCREEN_HEIGHT/11)*(i-1)/2+34, SCREEN_WIDTH/12, SCREEN_HEIGHT/12};
SDL_SetRenderDrawColor( gRenderer, 0xff, 0xcc, 0x66, 0xFF );
SDL_RenderFillRect( gRenderer, &fillRect );
}
else{
if (aa.p2[0]==i && aa.p2[1]==j){ // FICHA 2
SDL_Rect fillRect = {(50+SCREEN_WIDTH/11)+(SCREEN_WIDTH/11)*(j-1)/2+34, (SCREEN_HEIGHT/11)+(SCREEN_HEIGHT/11)*(i-1)/2+34, SCREEN_WIDTH/12, SCREEN_HEIGHT/12};
SDL_SetRenderDrawColor( gRenderer, 0x6E, 0x2C, 0x67, 0xFF );
SDL_RenderFillRect( gRenderer, &fillRect );
}
else{ // CASILLA VACÍA
SDL_Rect fillRect = {(50+SCREEN_WIDTH/11)+(SCREEN_WIDTH/11)*(j-1)/2+34, (SCREEN_HEIGHT/11)+(SCREEN_HEIGHT/11)*(i-1)/2+34, SCREEN_WIDTH/12, SCREEN_HEIGHT/12};
SDL_SetRenderDrawColor( gRenderer, 0x8B, 0x45, 0x13, 0xFF);
SDL_RenderFillRect( gRenderer, &fillRect );
}
}
}
}
}
}
SDL_RenderPresent( gRenderer );
}
loadMedia():
bool loadMedia()
{
//Loading success flag
bool success = true;
//Nothing to load
return success;
}
I want to have both on screen. To load the image to be left in a part of the screen forever and then update the rectangles generated with gimprimir().
SDL_BlitSurface is the SDL function used to blit (copy) from a surface to another, it takes two SDL_Rect arguments, srcrect for source and dstrect for destination.
SDL_BlitSurface isn't changed much from SDL1.2 to SDL2, so, old examples which use it should still work.
In the getting_an_image_on_the_screen example you propose a NULL pointer is used for both src and dst rect, a NULL rect mean copy the whole src surface into the destination at top, left = 0, 0 with a maximum size equal to the destination size (if the source is bigger should be cropped).
To blit a partial image just specify src and dst rect:
SDL_Rect srcrect = { 0, 0, 100, 100 };
SDL_Rect dstrect = { 50, 50, 100, 100 };
//Apply the image
SDL_BlitSurface( gHelloWorld, &srcrect, gScreenSurface, &dstrect );
In this case top, left = 0, 0 with width, height = 100, 100 from source surface (gHelloWorld) is blit to top, left = 50, 50 of dest surface (gScreenSurface).
how to load it with other types of objects
Sorry but is not clear to me what you mean by other types of objects
SDL_BlitSurface() will work, but since you're using SDL2 you're probably working mostly with SDL_Textures and not SDL_Surfaces. If this is the case the easiest way to copy one texture onto another texture or onto the screen is using SDL_RenderCopy() as keltar mentioned. The advantages of using SDL_Texture are explained here: http://wiki.libsdl.org/MigrationGuide#Video
As documented here, SDL_RenderCopy() takes four arguments: a pointer to the SDL_Renderer, a pointer to your source texture, a pointer to the source rectangle, and a pointer to the destination rectangle. By default the texture will be copied onto the screen but you can copy onto other textures by setting a target texture with SDL_SetRenderTarget().
Everything about source and destination rectangles carries over from SDL_BlitSurface.
Related
Today I learned about "characters" which are made from more than one code point in UTF8. I always believed that one code point in UTF8 maps to a specific character, but it seems like I was wrong.
For example, the following single glyph "é" consists of 3 bytes making up 2 code points.
I am having trouble to render this symbol correctly using SDL_ttf. It seems to use the FT_Get_Char_Index function from the freetype library to find the glyph. It does so by passing the code point, and the library treats it as if it was more than one glyph. How would I use the freetype library to render this glyph correctly?
TTF_Font *ttf = TTF_OpenFont("C:\\UbuntuMono-Regular.ttf", 24);
SDL_Color color = {0, 255, 255, 255};
SDL_Surface *surface = TTF_RenderUTF8_Blended(ttf, "é", color); // u8"é" doesn't work neither
Here's how it looks:
I wasn't able to reproduce your problem. Could it be a problem in your font?
This is a minimal example I made (just change fontFile to the path to your font):
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
int main()
{
const SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
SDL_Window* window;
SDL_Renderer* renderer;
SDL_CreateWindowAndRenderer(200, 200, 0, &window, &renderer);
TTF_Init();
const char* fontFile = "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf";
TTF_Font *font = TTF_OpenFont(fontFile, 32);
SDL_Surface* surface = TTF_RenderUTF8_Blended(font, "é", white);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect rect = {10, 10, surface->w, surface->h};
while(1)
{
SDL_Event event;
SDL_WaitEvent(&event);
if(event.type == SDL_QUIT)
break;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, 0, &rect);
SDL_RenderPresent(renderer);
}
}
This is the output I get:
I'm using C.
I always believed that one code point in UTF8 maps to a specific
character, but it seems like I was wrong. For example, the following
single glyph "é" consists of 3 bytes making up 2 code points.
It's true that a few codepoints can be grouped to make one "character" (the correct name is grapheme)(link). But I don't think it's the case for é (link). Although I might be wrong about that.
im trying to show two different animations made with two functions, one render and two threads.
Im getting different kind of error each time i run the code.
Such as: segmentation default in the refresh function (line SDL_RenderPresent(w1.renderer);) and other strange errors too. Everytime is different.
I know its about using the render in both different threads. But i dont know how to solve this problem.
When i run with just one of the threads everything its ok. But not with the two of them.
I just want to show different graphics playing around the window. (always using sdl2).
Here is my code:
window.h:
class Window
{
public:
SDL_Window *window1;
int background,windowWidth,windowHeight,windowXcoord,windowYcoord;
SDL_Renderer * renderer;
SDL_Renderer * renderer2;
Window();
};
void refresh();
extern Window w1;
window.cpp:
Window w1;
Window::Window()
{
window1=nullptr;
background = 0xffffff;
windowWidth=700; windowHeight=500;
windowXcoord = 650; windowYcoord = 0;
this->window1 =
SDL_CreateWindow("Window",windowXcoord,windowYcoord,
windowWidth,windowHeight, SDL_WINDOW_SHOWN);
this->renderer = SDL_CreateRenderer( this->window1, -1 ,
SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor( this->renderer, 255, 255, 255, 255 );
SDL_RenderClear( this->renderer );
SDL_RenderPresent(this->renderer);
}
// this function is called everytime an object or function want to "refresh" the window.
void refresh(){
// clearing the window
SDL_SetRenderDrawColor( w1.renderer, 255, 255, 255,0 );
SDL_RenderClear( w1.renderer );
// r1, the first Rects object to animate.
SDL_SetRenderDrawColor( w1.renderer,255,0,0,255);
SDL_RenderFillRect( w1.renderer, &r1.rect );
// r2, the second Rects.
SDL_SetRenderDrawColor( w1.renderer,255,0,255,255);
SDL_RenderFillRect( w1.renderer, &r2.rect );
SDL_RenderPresent(w1.renderer);
}
rects.h:
class Rects
{
public:
SDL_Rect rect;
Rects();
};
extern Rects r1, r2;
void moveR1();
void moveR2();
rects.cpp:
Rects::Rects()
{ }
Rects r1,r2;
// moveR1 and moveR2, (just some dumm animations-example)
void moveR1(){
r1.rect.x=400; r1.rect.y=100;
r1.rect.w =40; r1.rect.h =10;
SDL_SetRenderDrawColor( w1.renderer,255,0,0,255);
SDL_RenderFillRect( w1.renderer, &r1.rect );
{
for (int u = 1; u<=5;u++){
r1.rect.x=200; r1.rect.y=100;
r1.rect.w =50; r1.rect.h =50;
refresh();
for (int i = 1; i <=200; i++){
r1.rect.x-=1;
usleep (7000);
refresh();
}
}
}
}
void moveR2(){
r2.rect.x=200; r2.rect.y=100;
r2.rect.w =40; r2.rect.h =10;
SDL_SetRenderDrawColor( w1.renderer,255,255,0,255);
SDL_RenderFillRect( w1.renderer, &r2.rect );
{
for (int u = 1; u<=5;u++){
check
r2.rect.x=200; r2.rect.y=100;
r2.rect.w =50; r2.rect.h =50;
refresh();
for (int i = 1; i <=200; i++){
r2.rect.x+=1; r2.rect.y+=1;
usleep (7000);
refresh();
}
}
}
}
and the main:
int main()
{
thread t1(moveR1);
t1.detach();
thread t2(moveR2);
t2.detach();
// sleep instead of showing a loop.
sleep (10);
return 0;
}
any polite help is welcome, im here to learn.
When i run with just one of the threads everything its ok. But not with the two of them.
According to the documentation for 2D accelerated rendering (also known as SDL_render.h, in other terms the place where SDL_RenderPresent lives):
This API is not designed to be used from multiple threads, see SDL bug #1995 for details.
You won't be able to do that any time soon. By looking at the description of the error, it looks like an UB and the link to the bug gives you all the details probably.
I just want to show different graphics playing around the window.
You don't need to use two threads to do that.
You can just update positions, animations and whatever is needed for your graphics and show them from the same thread. Nothing prevents you from calling the same function (as an example SDL_RenderCopy) more than once before to invoke SDL_RenderPresent.
If you needed a thread per graphic, what about a game with thousands of polygons on video?
// thanks to skypjack, i am able to solve the problem
// HOW TO PRESENT TWO (OR MORE) GRAPHICS WITH SDL_RENDERER WITHOUT USING THREADS.
// The two rectangle animation are just and example.
// It could be whatever we want to show.
// In this case, just two rectangles moving around.
#include <unistd.h>
#include <SDL.h>
using namespace std;
SDL_Window *window1;
int background, windowWidth, windowHeight,windowXcoord,
windowYcoord;
SDL_Rect rectangle1;
SDL_Rect rectangle2;
SDL_Renderer *renderer;
int sequence1;
int sequence2;
void moveRectangle1(), moveRectangle2();
int main()
{
window1=nullptr;
background = 0xffffff;
windowWidth=700; windowHeight=500;
windowXcoord = 650; windowYcoord = 0;
window1 = SDL_CreateWindow("Main
Window",windowXcoord,windowYcoord, windowWidth,windowHeight,
SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer( window1, -1 ,
SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor( renderer, 255, 255, 255, 255 );
SDL_RenderClear( renderer );
SDL_RenderPresent(renderer);
rectangle1.x = 100; rectangle1.y = 100; rectangle1.h = 40;
rectangle1.w = 40;
rectangle2.x = 100; rectangle2.y = 100; rectangle2.h = 40;
rectangle2.w = 40;
for (int i = 1;i<500;i++){
SDL_SetRenderDrawColor( renderer, 255, 255, 255,0 );
SDL_RenderClear( renderer );
// chequear return (cerrar programa)
moveRectangle1();
moveRectangle2();
SDL_RenderPresent(renderer);
usleep (12000);
}
return 0;
}
void moveRectangle1(){
sequence1++;
if (sequence1 <= 50){ rectangle1.x++; }
else if (sequence1 <= 100){ rectangle1.x--; }
else { sequence1 = 0; }
SDL_SetRenderDrawColor(renderer,255,0,0,255);
SDL_RenderFillRect(renderer, &rectangle1 );
}
void moveRectangle2(){
sequence2++;
if (sequence2 <= 100){ rectangle2.y++; }
else if (sequence2 <= 200){ rectangle2.y--; }
else { sequence2 = 0; }
SDL_SetRenderDrawColor(renderer,0,200,0,255);
SDL_RenderFillRect( renderer, &rectangle2 );
}
I'm following this tutorial that teaches how to use SDL2 with the final goal of learning C++ in a more fun and interactive way.
For this, I only need to be able to draw lines, polygons and circles.
So, after reading part 1 that explains how to create a window on the screen and part 3 that introduces event handling, I headed torward part 7 and 8 that explain, respectively, how to create a renderer and how to draw a rectangle on the screen. This is the code I've got so far (it isn't exactly the same as the code on the tutorial: I've introduced a struct to pass SDL objects around and removed all the error handling which was confusing):
#include <SDL2/SDL.h>
//screen dimensions costants
#define SCREEN_WIDTH 540
#define SCREEN_HEIGHT 960
//data structure holding the objects needed to create a window and draw on it
struct interface {
SDL_Window * window = NULL;
SDL_Surface * surface = NULL;
SDL_Renderer * renderer = NULL;
};
//function which inits the sdl and creates an interface object
interface init() {
interface screen;
SDL_Init(SDL_INIT_VIDEO);
screen.window = SDL_CreateWindow("", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
screen.surface = SDL_GetWindowSurface(screen.window);
screen.renderer = SDL_CreateRenderer(screen.window, -1, SDL_RENDERER_ACCELERATED);
return screen;
}
//function to free the memory and close the sdl application
void close(interface screen) {
SDL_DestroyRenderer(screen.renderer);
SDL_DestroyWindow(screen.window);
screen.renderer = NULL;
screen.window = NULL;
SDL_Quit();
}
int main(int argc, char* args[]) {
//start the application
interface screen = init();
//setup for event handling
bool quit = false;
SDL_Event event;
//the shape to render
SDL_Rect fillRect = { SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 };
//main loop which first handles events
while (!quit) {
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT)
quit = true;
}
//should draw a red rectangle on the screen
SDL_SetRenderDrawColor(screen.renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(screen.renderer);
SDL_SetRenderDrawColor(screen.renderer, 0xFF, 0x00, 0x00, 0xFF);
SDL_RenderFillRect(screen.renderer, &fillRect);
}
//End the application
close(screen);
return 0;
}
The problem is that, as it is, the program draws nothing to the screen (which remains black), and if I remove the line screen.surface = SDL_GetWindowSurface(screen.window); it also begins lagging a lot in a manner I even find difficult to exit the application.
Note that I'm programming on Android using C4droid and the SDL Plugin for C4droid.
Why is that happening? What am I doing wrong?
EDIT Problem solved by renaming close to end and by including a call to SDL_RenderPresent(screen.renderer); at the end of the main loop. With this setup the screen surface has to be deleted or the program won't draw anything.
Thanks to #keltar and #Wutipong Wongsakuldej for answering the question in the comments
First of all I tested the code in Windows (MSYS2) rather than on Android as I don't have AIDE installed at the moment.
Basically I added 2 lines of code into the main loop :
//main loop which first handles events
while (!quit) {
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT)
quit = true;
}
//should draw a red rectangle on the screen
SDL_SetRenderDrawColor(screen.renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(screen.renderer);
SDL_SetRenderDrawColor(screen.renderer, 0xFF, 0x00, 0x00, 0xFF);
SDL_RenderFillRect(screen.renderer, &fillRect);
/** below lines are added **/
SDL_RenderPresent(screen.renderer);
SDL_Delay(0);
}
SDL_RenderPresent draw whatever you've renderred so far to the screen. This make the output shows up.
SDL_Delay() this is added to give the cpu time back to the os. Without this your app might become unresponsive and the cpu utilization will be 100% (at one core) in some operating system (especially the very old one). I don't know if this is needed in Android or not anyway. Give it a try.
my game has a background and a enemy that move.every time my enemy move i want to fill the last move but when i use SDL_FillRect it remove enemy with the background.but i want to have the background.
int main( int argc, char* args[] )
{
towers mytowers;
enemy myenemy;
bool quit = false;
bool backgroundflag=false;
init();
load_files();
Mix_PlayMusic( music, -1 );
while(castelhealth!=0)
{
mytowers.showtower();
myenemy.path();
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
}
clean_up();
return 0;
}
Im new to SDL and C++ overall.
However when i do DisplayFormat on an image for faster blitting it makes it an rectangle.
SDL_Surface* tempImage = NULL;
// The image that will be used (optimized)
image = NULL;
image = IMG_Load( filename.c_str() );
if ( tempImage != NULL )
{
// Create optimization
image = SDL_DisplayFormat( tempImage ); // Makes the circle an rectangle
// Free the old image
SDL_FreeSurface( tempImage );
}
Why is that? If i dont do DisplayFormat, the circle remains an circle when blitted.
This is because your display format which you're converting your image to does not support transparent pixels. You must set your video mode to have 32 bits per pixel, like below:
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface *window = SDL_SetVideoMode(width, height, 32, flags);
// ...
You also need to change SDL_DisplayFormat to SDL_DisplayFormatAlpha.