Ui Container added, Sprite richtung bug komisch hmmmmm

main
Jan 3 years ago
parent 732efea2d3
commit 94a3b03a1a

@ -1,5 +1,4 @@
{
"C_Cpp.errorSquiggles": "Enabled",
"files.associations": {
"isometricrenderer.h": "c",
"sprite.h": "c",
@ -10,6 +9,10 @@
"tile.h": "c",
"raylib.h": "c",
"game.h": "c",
"buttons.h": "c"
}
"buttons.h": "c",
"string.h": "c",
"uicontainer.h": "c",
"button.h": "c"
},
"C_Cpp.errorSquiggles": "disabled"
}

@ -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 buttons.o
OBJS = main.o sprite.o inputHandler.o isometricMap.o list.o game.o textureatlas.o animation.o animationHandler.o bucket.o mergeSort.o button.o uiContainer.o
spiel: $(OBJS)
$(CC) -o spiel $(OBJS) $(FLAGS)
@ -38,8 +38,11 @@ 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)
button.o: Ui/button.c
$(CC) -c Ui/button.c $(FLAGS)
uiContainer.o: Ui/uiContainer.c
$(CC) -c Ui/uiContainer.c $(FLAGS)
clean:
rm *.o spiel

@ -22,29 +22,9 @@ Fantasy Welt oder Realistisch?
+ LinkedList erweitern
+ Sprites Animationen etc improven
+ Die Inputs sollten den Kamera Zoom beachten, aktuell geht noch alles kaputt wenn man den zoom umstellt
+ Funktion, um die ganzen Sprites nach ihrer y-Koordinaten sortiert zu drawen
+ Drawable Container machen, die sortiert werden können, dort kommen alle Tiles und Sprites rein, damit sie dann sortiert werden können
+ Maps in eigenen Dateien speichern
+ Parser für Map-Dateien
+ MapEditor
* Rendering Reihenfolge: layer 0, Sprites auf layer 0, layer 1, Sprites auf layer 1; Theoretisch müssen die einzelnen Layer Reihenweise gedrawed werden mit den Sprites zwischendrin
+ IsometricMap struct erstellen, das den IsometricMap(+Layer) Array speichert ?
+ Beim rendern müssen die map tiles und die sprites nach ihrer depth (d = x + y + ~0.05*z) sortiert werden, dafür sollten wir ein bucket sorting system implementieren. Buckets sollten erstmal nur tiles und sprites unterstützen.
+ Wir können auch die Sprites in der Liste nach d sortieren, dann geht das gut mit "nur in der Kamera sichtbare Sprites rendern". d ist nicht optimal, vielleicht auch einfach über die ganze Liste gehen und nur die sprites mit $ x y \subset camBounds $ in das Bucket sorting system einfügen. Buckets können auch mit MergeSort oder so sortiert werden
[Vererbung in C, drawable superStruct?](https://de.wikibooks.org/wiki/C%2B%2B-Programmierung:_Vererbung)
+ Mit der Map geht das schon recht einfach, weil wir wissen welche tiles in der Kamera sind. d
+ TODO: Das rendern der IsometricMap wird bei größeren Maps sehr ineffizient;
+ Add offset x and y to each tile to be calculated ONCE, not every frame
+ Alle gehardcodeten screen bounds (450 225 800 400) durch GetScreenWidth() GetScreenHeight() ersetzen
+ Kameraposition abhängiges drawen auch für Sprites implementieren
### WiP
+ Dokumentation aufholen
### Done
+ Movement speed der Sprites an delta time orientieren

@ -1,10 +1,29 @@
#include "buttons.h"
#include "button.h"
#include "../game.h"
#include "screenIDs.h"
#include "stdio.h"
#include "stdlib.h"
#include "raylib.h"
#include "string.h"
void executeButton(Button *button, Game *game){
Button * InitButton(Texture2D textures[3], Vector2 *position, char *text, int textLEN, int id){
Button *button = malloc(sizeof(Button));
button->textures[0] = textures[0];
button->textures[1] = textures[1];
button->textures[2] = textures[2];
button->position = (Vector2){position->x, position->y};
button->centerPosition = (Vector2){position->x + textures[0].width/2, position->y + textures[0].height/2};
strncpy(button->text, text, textLEN);
button->state = 0;
button->id = id;
return button;
}
void ButtonExecuteButton(Button *button, Game *game){
button->state = 0;
switch(button->id){
case 0: // continue game
@ -15,19 +34,17 @@ void executeButton(Button *button, Game *game){
}
}
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");
void ButtonDrawButton(Button * button){
ButtonUpdateButtonState(button);
DrawTexture(button->textures[button->state], button->position.x, button->position.y, WHITE);
DrawText(button->text, button->centerPosition.x - MeasureText(button->text, BUTTON_FONT_SIZE)/2, button->centerPosition.y - BUTTON_FONT_SIZE/2, BUTTON_FONT_SIZE, BLACK);
}
int updateButtonState(Button * button){
int ButtonUpdateButtonState(Button * button){
if(GetMouseX() > button->position.x &&
GetMouseX() < button->position.x + button->textures[button->state]->width &&
GetMouseX() < button->position.x + button->textures[button->state].width &&
GetMouseY() > button->position.y &&
GetMouseY() < button->position.y + button->textures[button->state]->height
GetMouseY() < button->position.y + button->textures[button->state].height
){
if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){
button->state = 2;
@ -40,22 +57,22 @@ int updateButtonState(Button * button){
return 0;
}
int isButtonHovered(Button * button){
int ButtonIsButtonHovered(Button * button){
if(GetMouseX() > button->position.x &&
GetMouseX() < button->position.x + button->textures[0]->width &&
GetMouseX() < button->position.x + button->textures[0].width &&
GetMouseY() > button->position.y &&
GetMouseY() < button->position.y + button->textures[0]->height
GetMouseY() < button->position.y + button->textures[0].height
){
return button->state = 1;
}
return button->state = 0;
}
int isButtonPressed(Button * button){
int ButtonIsButtonPressed(Button * button){
if(GetMouseX() > button->position.x &&
GetMouseX() < button->position.x + button->textures[0]->width &&
GetMouseX() < button->position.x + button->textures[0].width &&
GetMouseY() > button->position.y &&
GetMouseY() < button->position.y + button->textures[0]->height
GetMouseY() < button->position.y + button->textures[0].height
){
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
return button->state = 2;

@ -0,0 +1,31 @@
#ifndef BUTTONS_H_
#define BUTTONS_H_
#include "raylib.h"
#include "../game.h"
#define BUTTON_FONT_SIZE 36
typedef struct Button{
Texture2D textures[3]; // [0]: Normal [1]: Hovered [2]: Pressed
Vector2 position;
Vector2 centerPosition;
char text[20];
int state; // 0: default 1: hovered 2: pressed
int id;
} Button;
Button * InitButton(Texture2D textures[3], Vector2 *position, char *text, int textLEN, int id);
// executes the logic of one button of certain id - huge switch?
void ButtonExecuteButton(Button *button, Game * game);
int ButtonUpdateButtonState(Button * button);
void ButtonDrawButton(Button * button);
int ButtonisButtonPressed(Button * button);
int ButtonisButtonHovered(Button * button);
#endif

@ -1,26 +0,0 @@
#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

@ -0,0 +1,42 @@
#include "uiContainer.h"
#include "../game.h"
#include "button.h"
#include "raylib.h"
#include "stdlib.h"
#include "stdio.h"
UiContainer * UiContainerInitPauseUiContainer(){
UiContainer *uiContainer = malloc(sizeof(UiContainer));
Texture2D textures[3] = { LoadTexture("assets/button.png"),
LoadTexture("assets/button_hovered.png"),
LoadTexture("assets/button_pressed.png")};
Vector2 position = (Vector2){GetScreenWidth()/2 - textures[0].width/2, GetScreenHeight()/2 + 150};
Button *continuebutton = InitButton(textures, &position, "Continue", 9, 0);
uiContainer->buttons[0] = continuebutton;
uiContainer->buttonCounter = 1;
// Methode funktioniert wieso auch immer auch ohne dieses return. C returned Implizit odder was O_o
return uiContainer;
}
UiContainer * UiContainerInitGameUiContainer(){
}
void UiContainerUpdateUiContainer(UiContainer *uiContainer, Game *game){
int i = 0;
for(i=0 ; i < uiContainer->buttonCounter; i++){
ButtonUpdateButtonState(uiContainer->buttons[i]);
if(uiContainer->buttons[i]->state == 2){
ButtonExecuteButton(uiContainer->buttons[i], game);
}
}
}
void UiContainerDrawUiContainer(UiContainer *uiContainer){
int i = 0;
for(i=0 ; i < uiContainer->buttonCounter; i++){
ButtonDrawButton(uiContainer->buttons[i]);
}
}

@ -0,0 +1,21 @@
#ifndef UICONTAINER_H_
#define UICONTAINER_H_
#include "raylib.h"
#include "button.h"
#include "../game.h"
typedef struct UiContainer{
Button *buttons[15];
int buttonCounter;
} UiContainer;
// executes the logic of one button of certain id - huge switch?
void UiContainerUpdateUiContainer(UiContainer *uiContainer, Game *game);
void UiContainerDrawUiContainer(UiContainer *uiContainer);
UiContainer * UiContainerInitPauseUiContainer();
UiContainer * UiContainerInitGameUiContainer();
#endif

@ -1,4 +1,5 @@
#include "raylib.h"
#include "string.h"
#include <stdio.h>
#include <stdlib.h>
#include "sprite.h"
@ -10,74 +11,24 @@
#include "DepthSorting/bucket.h"
#include "DepthSorting/mergeSort.h"
#include "Ui/screenIDs.h"
#include "Ui/buttons.h"
#include "Ui/button.h"
#include "Ui/uiContainer.h"
int main(){
InitWindow(1600, 900, "basic window");
InitWindow(1280, 720, "basic window");
Game *game = GameInit();
// TODO: Screen structs, die zum Beispiel die UiContainer enthalten?
UiContainer *pauseScreenUiContainer = UiContainerInitPauseUiContainer();
// Hides the operating systems own cursor
HideCursor();
SetTargetFPS(60);
return uitest(game);
// GAME MAIN ROUTINE
while(!WindowShouldClose()){
ListActAllSprites(game);
ClearBackground(RAYWHITE);
BeginDrawing();
BeginMode2D(*(game->camera));
//IsometricRendererRenderIsometricMap(game);
//ListDrawAllSprites(game->sprites, game->layers, game->camera);
IsometricMapDraw(game);
EndMode2D();
// Moving cursor Sprite to Mouse Pos and drawing it
game->cursorSprite->x = game->inputHandler->cursorPos.x;
game->cursorSprite->y = game->inputHandler->cursorPos.y;
DrawSpriteToScreen(game->cursorSprite);
// User Input Handling
mouseInput(game);
keyboardInput(game->inputHandler, game->camera);
DrawFPS(GetScreenWidth() - 95, 10);
EndDrawing();
}
CloseWindow();
return 0;
}
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
@ -137,12 +88,8 @@ int uitest(Game * game){
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);
}
UiContainerUpdateUiContainer(pauseScreenUiContainer, game);
UiContainerDrawUiContainer(pauseScreenUiContainer);
DrawSpriteToScreen(game->cursorSprite);
DrawFPS(GetScreenWidth() - 95, 10);
@ -166,4 +113,6 @@ int uitest(Game * game){
CloseWindow();
return 0;
}
}

Loading…
Cancel
Save