segfault fixed

main
Jan 3 years ago
parent f84124e768
commit 732efea2d3

@ -9,6 +9,7 @@
"textureids.h": "c",
"tile.h": "c",
"raylib.h": "c",
"game.h": "c"
"game.h": "c",
"buttons.h": "c"
}
}

@ -1,6 +1,6 @@
CC = gcc
FLAGS = -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
OBJS = main.o sprite.o inputHandler.o isometricMap.o list.o game.o textureatlas.o animation.o animationHandler.o bucket.o mergeSort.o
OBJS = main.o sprite.o inputHandler.o isometricMap.o list.o game.o textureatlas.o animation.o animationHandler.o bucket.o mergeSort.o buttons.o
spiel: $(OBJS)
$(CC) -o spiel $(OBJS) $(FLAGS)
@ -38,5 +38,8 @@ bucket.o: DepthSorting/bucket.c
mergeSort.o: DepthSorting/mergeSort.c
$(CC) -c DepthSorting/mergeSort.c $(FLAGS)
buttons.o: Ui/buttons.c
$(CC) -c Ui/buttons.c $(FLAGS)
clean:
rm *.o spiel

@ -0,0 +1,66 @@
#include "buttons.h"
#include "../game.h"
#include "screenIDs.h"
#include "stdio.h"
#include "raylib.h"
void executeButton(Button *button, Game *game){
button->state = 0;
switch(button->id){
case 0: // continue game
if(game->screen == SCREEN_PAUSE){
game->screen = SCREEN_GAME;
}
break;
}
}
void drawButton(Button * button){
printf("%d\n", button->state);
updateButtonState(button);
printf("DRAW JETZT\n");
DrawTexture(*button->textures[button->state], button->position.x, button->position.y, WHITE);
printf("funktioniert\n");
}
int updateButtonState(Button * button){
if(GetMouseX() > button->position.x &&
GetMouseX() < button->position.x + button->textures[button->state]->width &&
GetMouseY() > button->position.y &&
GetMouseY() < button->position.y + button->textures[button->state]->height
){
if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){
button->state = 2;
return 2;
}
button->state = 1;
return 1;
}
button->state = 0;
return 0;
}
int isButtonHovered(Button * button){
if(GetMouseX() > button->position.x &&
GetMouseX() < button->position.x + button->textures[0]->width &&
GetMouseY() > button->position.y &&
GetMouseY() < button->position.y + button->textures[0]->height
){
return button->state = 1;
}
return button->state = 0;
}
int isButtonPressed(Button * button){
if(GetMouseX() > button->position.x &&
GetMouseX() < button->position.x + button->textures[0]->width &&
GetMouseY() > button->position.y &&
GetMouseY() < button->position.y + button->textures[0]->height
){
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
return button->state = 2;
}
}
return button->state = 0;
}

@ -0,0 +1,26 @@
#ifndef BUTTONS_H_
#define BUTTONS_H_
#include "raylib.h"
#include "../game.h"
typedef struct Button{
Texture2D *textures[3]; // [0]: Normal [1]: Hovered [2]: Pressed
Vector2 position;
//char text[20];
int state; // 0: default 1: hovered 2: pressed
int id;
} Button;
// executes the logic of one button of certain id - huge switch?
void executeButton(Button *button, Game * game);
int updateButtonState(Button * button);
void drawButton(Button * button);
int isButtonPressed(Button * button);
int isButtonHovered(Button * button);
#endif

@ -1,5 +1,5 @@
#ifndef BUTTONIDS_H_
#define BUTTONIDS_H_
#ifndef SCREENIDS_H_
#define SCREENIDS_H_
#define SCREEN_EXIT 0
#define SCREEN_MAINMENU 1

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

@ -10,10 +10,11 @@
#include "DepthSorting/bucket.h"
#include "DepthSorting/mergeSort.h"
#include "Ui/screenIDs.h"
#include "Ui/buttons.h"
int main(){
InitWindow(800, 450, "basic window");
InitWindow(1600, 900, "basic window");
Game *game = GameInit();
@ -64,6 +65,19 @@ int main(){
int uitest(Game * game){
Texture2D button = LoadTexture("assets/button.png");
Texture2D buttonHovered = LoadTexture("assets/button_hovered.png");
Texture2D buttonPressed = LoadTexture("assets/button_pressed.png");
Button continuebutton;
continuebutton.id = 0;
continuebutton.position = (Vector2){GetScreenWidth()/2, GetScreenHeight()/2};
continuebutton.state = 0;
continuebutton.textures[0] = &button;
continuebutton.textures[1] = &buttonHovered;
continuebutton.textures[2] = &buttonPressed;
while(!WindowShouldClose()){
// Moving cursor Sprite to Mouse Pos
@ -111,9 +125,25 @@ int uitest(Game * game){
break;
case SCREEN_PAUSE:
ClearBackground(RAYWHITE);
BeginDrawing();
BeginMode2D(*(game->camera));
IsometricMapDraw(game);
EndMode2D();
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), (Color){0, 0, 0, 150});
int textWidthHalf = MeasureText("Paused", 28) / 2;
DrawText("Paused", GetScreenWidth()/2 - textWidthHalf, GetScreenHeight()/2 - 14, 28, WHITE);
printf("Vorher \n");
drawButton(&continuebutton);
printf("Nachher \n");
if(continuebutton.state == 2){
executeButton(&continuebutton, game);
}
DrawSpriteToScreen(game->cursorSprite);
DrawFPS(GetScreenWidth() - 95, 10);
@ -123,6 +153,10 @@ int uitest(Game * game){
game->screen = SCREEN_GAME;
}
break;
default:
printf("\n\n\n\n\n\n Wir haben ein problematisches Problem! Die Screen-ID [%d] ist nicht definiert. Hmmpf... früher bescheid wisse!\n\n\n\n\n\n", game->screen);
return 1;
break;
}

Loading…
Cancel
Save