Buttons im Feld darstellen und anklicken

Guten Tag

Nach einer gefühlten Ewigkeit und ein bisschen spicken bei google und co ist es mir nun endlich gelungen, ein Feld (20x20) mit kleinen Buttons darzustellen welche man anklicken kann und sie wieder verschwinden

Doch leider werden die Bilder jetzt nur noch am Rand gerendert… was mache ich falsch ?

Es muss an der System_var mit dem bool in main liegen.. kann den Fehler aber nicht finden

[cpp]

#include <SDL2/SDL.h>

#include <stdio.h>
#include <stdlib.h>

#define hWnd_Button_width 40
#define hWnd_Button_height 40

int i,j; //

int mouse_x;
int mouse_y;

SDL_Rect dstrect;
SDL_Rect rects[20][20];

//________________

SDL_Rect newSDL_Rect(int xs, int ys, int widths, int heights)
{
SDL_Rect rectangular;

rectangular.x = xs + 120;
rectangular.y = ys;

rectangular.w = 20 ;
rectangular.h = 20;

return rectangular;

}

//________________

int main (int argc, char** argv)
{

SDL_Event event;
SDL_Init(SDL_INIT_EVERYTHING);


// create windows
SDL_Window *window = SDL_CreateWindow("button test", 0,
									  0, 720, 1560, SDL_WINDOW_SHOWN);

// create renderer
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);

SDL_Rect r;
r.x = 550;
r.y = 50;
r.w = 50;
r.h = 50;

//-------------------------------------------------

int quit = 1;
int system_var = 1;

bool ButtonisActive[i][j];

//-------------------------------------------------

while(quit == 1) // prgramm loop
{

// SDL_WaitEvent(&event);

while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
quit = 0;
break;
}

  if (event.type == SDL_MOUSEMOTION)     
 {     
   mouse_x = event.motion.x;	
   mouse_y = event.motion.y;     
  //SDL_RenderDrawPoint(renderer, r.x, r.y);   
 }

//-------------------------------------------------

//case SDL_MOUSEBUTTONDOWN:

if (event.type == SDL_MOUSEBUTTONDOWN)
{
for (int i = 0; i < 12; i++)
for (int j = 0; j < 20; j++)
{
if (ButtonisActive[i][j]== true)
{
// crazy collisions code
if(((( mouse_x + 6 >= rects[i][j].x) &&
(mouse_x + 6<= rects[i][j].x +
hWnd_Button_width)) ||

((rects[i][j].x + hWnd_Button_width>= mouse_x) &&
(rects[i][j].x + hWnd_Button_width <= mouse_x + 6))) &&

(((mouse_y + 12 >= rects[i][j].y)&&
(mouse_y + 12 <= rects[i][j].y + hWnd_Button_height)) ||

((rects[i][j].y + hWnd_Button_height>= mouse_y) &&
(rects[i][j].y + hWnd_Button_height <= mouse_y+12 ))))
{
// collision

// quit = 0;
ButtonisActive[i][j]= false;
break;
}

}
}
}

}

//---------------------------------

SDL_SetRenderDrawColor( renderer, 0, 0,0, 0 );

// Clear winow

SDL_RenderClear( renderer );

SDL_SetRenderDrawColor( renderer, 255, 255,255, 0 );

//---------------------------------

if( system_var == 1)
{
for (int i = 0; i < 12; i++)
for (int j = 0; j < 20; j++)
{

if (ButtonisActive[i][j]== false)
{

rects[i][j] = newSDL_Rect(80*i,  80*j, 
1, 1);

// arrayofEnemy[i].x_Enemy += rand()%800+1;
// arrayofEnemy[i].y_Enemy += rand()%200+27;

ButtonisActive[i][j]= true;
}
}
system_var = 2;
}

//---------------------------------

if( system_var == 2)
{
for (int i = 0; i < 12; i++)
for (int j = 0; j < 20; j++)
{
if (ButtonisActive[i][j]== true)
{
// SDL_RenderDrawPoint(renderer, r.x, r.y);
SDL_RenderFillRect( renderer, &rects[i][j] );

//SDL_Delay(100);
}
}

}

//---------------------------------

// Render the rect to the screen
SDL_RenderPresent(renderer);

}

SDL_DestroyWindow(window);
SDL_Quit();

return EXIT_SUCCESS;

}

[/cpp]

Und so…verschwindet die ganze Zeile beim drauf drücken wird aber ganz komplett gerendert

Two problems with the declaration of ButtonisActive.

bool ButtonisActive[i][j];
  1. i and j are uninitialized at this point. Would suggest you use some named constants for the sizes instead (these would also be useful in the loops).
  2. The values of ButtonisActive are left uninitialized (and are still uninitialized later when you start reading them).

Bool ButtonisActive[20][20];

War es :slight_smile:

habt 1000 dank !!!

→ jetzt kann ich spiele entwickeln, nicht bloß sachen darstellen, sondern clonen, abtippen und andres zeugs GEIL!