# Buttons im Feld darstellen und anklicken

**URL:** <https://discourse.libsdl.org/t/buttons-im-feld-darstellen-und-anklicken/65298>\
**Category:** Game Development\
**Created:** [December 21, 2025, 11:22am UTC](https://discourse.libsdl.org/t/buttons-im-feld-darstellen-und-anklicken/65298 "2025-12-21T11:22:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patty1991](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/2bfe46/32.png) [@Patty1991](https://discourse.libsdl.org/u/Patty1991)\
**Post date:** [December 21, 2025, 11:22am UTC](https://discourse.libsdl.org/t/buttons-im-feld-darstellen-und-anklicken/65298/1 "2025-12-21T11:22:23Z")

</div>

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;

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

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

return rectangular;

```

}

//\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

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

```auto
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;   
}

```auto
  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;  
}

}  
}  
}

}

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

```auto
SDL_SetRenderDrawColor( renderer, 0, 0,0, 0 );

// Clear winow

```

SDL\_RenderClear( renderer );

```auto
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)  
{

```auto
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);  
}  
}

}

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

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

```

}

```auto
SDL_DestroyWindow(window);
SDL_Quit();

return EXIT_SUCCESS;

```

}

[/cpp]

 ![Screenshot_20251221_122505_SDL plugin for C4droid](https://discourse.libsdl.org/uploads/default/original/2X/f/f8d8fd674aa0f0a6d21293326b3ee62f9b93469d.jpeg)

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

 ![Screenshot_20251221_123305_C4droid](https://discourse.libsdl.org/uploads/default/original/2X/2/288007eb81828a9d90ba79345e2e7ae5132e57b6.jpeg)

---

<div class="post-metadata">

**Author:** ![Peter87](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/f07891/32.png) [@Peter87](https://discourse.libsdl.org/u/Peter87)\
**Post date:** [December 21, 2025, 3:10pm UTC](https://discourse.libsdl.org/t/buttons-im-feld-darstellen-und-anklicken/65298/3 "2025-12-21T15:10:27Z")

</div>

Two problems with the declaration of `ButtonisActive`.

```c
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).

---

<div class="post-metadata">

**Author:** ![Patty1991](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/2bfe46/32.png) [@Patty1991](https://discourse.libsdl.org/u/Patty1991)\
**Post date:** [December 22, 2025, 1:16pm UTC](https://discourse.libsdl.org/t/buttons-im-feld-darstellen-und-anklicken/65298/4 "2025-12-22T13:16:28Z")

</div>

Bool ButtonisActive[20][20];

War es 🙂

habt 1000 dank !!!

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