Sign in to follow this  

Problem with button

This topic is 2542 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Introduction
I'm there again - hooray! I have the same program as the last time, but with some changes. Now I have one button with text. I need this: When I will click on the button, script will go off.

Code:

#include <string>
#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include <fstream>

#define SIRKA 240
#define VYSKA 320

using namespace std;

/***************************************/
/* FUNKCE PRO PRACI S SDL A KNIHOVNAMI */
/***************************************/

// Nacte vsechny komponenty SDL
bool K_LoadSDL() {
if (SDL_Init(SDL_INIT_EVERYTHING) == -1){
cout << "Nezdarilo se nacist SDL: " << SDL_GetError() << endl;
return false;
SDL_Quit();
}
return true;
}

// Nacte SDL_TTF
bool K_LoadTTF() {
if (TTF_Init() == -1) {
cout << "Nezdarilo se nacist TTF: " << TTF_GetError() << endl;
return false;
SDL_Quit();
TTF_Quit();
}
return true;
}


/**********************************/
/* FUNKCE PRO PRACI S OBRAZOVKAMI */
/**********************************/

// Nacte objekt na obrazovku
bool K_AddToScreen(int x, int y, SDL_Surface* source, SDL_Surface* destination) {
SDL_Rect umisteni;

umisteni.x = x;
umisteni.y = y;

if (SDL_BlitSurface(source, NULL, destination, &umisteni) == -1){
cout << "Nezdarilo se nacist obrazek na obrazovku: " << SDL_GetError() << endl;
return false;
}
return true;
}

// Obnovi obrazovku
bool K_ReloadScreen(SDL_Surface* screenName) {
if (SDL_Flip(screenName) == -1) {
cout << "Nezdarilo se obnovit obrazovku: " << SDL_GetError() << endl;
return false;
}
return true;
}


/**************************************************/
/* FUNKCE ZPRACOVAVAJICI AKCE V PROGRAMU (EVENTS) */
/**************************************************/

// Zjistí které tlačítko bylo stisknuto
//bool K_ButtonClicked(int x, int y) {
// if ((y < ry) && (x < rx)) {
// return true;
// } else {
// return false;
// }
//}

// Kontrola akci v programu
bool K_Process() {
SDL_Event event;
while(SDL_PollEvent(&event)){
switch (event.type) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
return true;
break;

default:
break;
}
case SDL_QUIT:
return true;
break;

case SDL_MOUSEBUTTONDOWN:
switch (event.button.button) {
case SDL_BUTTON_LEFT:
int x = event.button.x;
int y = event.button.y;

// Test pro tlacitko 1
if((0 < x < 120) && ( 0 < y < 106)) {
SDL_Quit();
}
break;
}
break;

default:
return false;
break;
}
}
}


// Funkce pro nacteni textu na obrazovku
// bool AddTextToScreen(int r, int g, int b, string font, string text, int size)

/****************************/
/* JEDNOTLIVA OKNA PROGRAMU */
/****************************/

// Hlavni okno programu
bool K_MainWindow() {
SDL_Surface* screen = NULL; // Obrazovka
SDL_Surface* message = NULL; // Text
SDL_Surface* button1 = NULL; // Tlacitko1

// Nacteni SDL
K_LoadSDL();

// Nacteni TTF
K_LoadTTF();

// Nacteni fontu
TTF_Font *font = NULL; // Font
font = TTF_OpenFont("lazy.ttf", 28);
if (!font) {
cout << "Nezdarilo se nacit font: " << TTF_GetError() << endl;
return false;
SDL_Quit();
TTF_Quit();
}
SDL_Color color = {0, 0, 0};
message = TTF_RenderText_Solid( font, "POKUS", color);

// Nacteni hlavni obrazovky + kontrola
screen = SDL_SetVideoMode(SIRKA, VYSKA, 32, SDL_SWSURFACE);

if (screen == NULL){
cout << "Nepodarilo se vytvorit okno: " << SDL_GetError() << endl;
return false;
SDL_Quit();
}

// Nacteni tlacitka 1
button1 = IMG_Load("./data/test.png");

// Nastaveni titulku okna
SDL_WM_SetCaption("Kubatura", NULL);

K_AddToScreen(0,0, button1, screen);
K_AddToScreen(5,5, message, screen);

bool done = false;
while(done != true){
done = K_Process();
// Obnoveni obrazovky
K_ReloadScreen( screen );
}
SDL_FreeSurface(message);
return true;
}

/***************/
/* FUNKCE MAIN */
/***************/
int main() {
K_MainWindow();
SDL_Quit();
return 0;
}


Compilation
# g++ -o kubatura.run kubatura.cpp -lSDL_ttf -lSDL_image `sdl-config --cflags --libs`


Compilation was without any problem.

Problem
1. Button has the same size as window (I can click everywhere to close program).
2. After closing, i get this error:
Neoprávněný přístup do paměti (SIGSEGV) (in english: Unauthorized access to memory (SIGSEGV)


Thanks for help. L.

Share this post


Link to post
Share on other sites
If is source code too long (for searching bugs), can you send there any code which create "button" in SDL?

UPDATE: I found the first problem:
if((0 < x < 120) && ( 0 < y < 106)) {

will be
if((x < 120) && (y < 106)) {

But I still don't know, where is problem with Unauthorized access to memory (SIGSEGV).

Share this post


Link to post
Share on other sites

This topic is 2542 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this