diff --git a/Input/inputHandler.c b/Input/inputHandler.c index 1618af9..2654a0a 100644 --- a/Input/inputHandler.c +++ b/Input/inputHandler.c @@ -62,22 +62,22 @@ void mouseInput(InputHandler *inputHandler, List *sprites, Texture2D *texture, C // resetting last selected Tile to grass texture int n = 9; if(inputHandler->selectedLayer != -1){ - IsometricMapChangeTextureIdOfTile(layers[inputHandler->selectedLayer], (int) inputHandler->selectedTile.x, (int) inputHandler->selectedTile.y, 0); + IsometricMapChangeTextureIdOfTile(layers[inputHandler->selectedLayer], (int) inputHandler->cursorWorldPos.x, (int) inputHandler->cursorWorldPos.y, 0); } - for(n = 2; n >= 0 ; n--){ + // TODO: n=2 no good style, but Segmentation fault when > layerAmount + for(n = 10; n >= 0 ; n--){ if(layers[n] != 0){ - IsometricMapProject(layers[n], camera, inputHandler->cursorPos.x, inputHandler->cursorPos.y, &inputHandler->selectedTile); + IsometricMapProject(layers[n], camera, inputHandler->cursorPos.x, inputHandler->cursorPos.y, &inputHandler->cursorWorldPos); - Tile *selectedTile = IsometricMapGetTileFromWorldCoordinates(layers[n], inputHandler->selectedTile.x, inputHandler->selectedTile.y); + Tile *selectedTile = IsometricMapGetTileFromWorldCoordinates(layers[n], inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y); if(selectedTile != 0){ - //printf("ALARM bei n = %d \n", n); - inputHandler->selectedTile.x = selectedTile->x; - inputHandler->selectedTile.y = selectedTile->y; + inputHandler->cursorWorldPos.x = selectedTile->x; + inputHandler->cursorWorldPos.y = selectedTile->y; // setting currently selected tile to tower inputHandler->selectedLayer = n; - IsometricMapChangeTextureIdOfTile(layers[n], (int) inputHandler->selectedTile.x, (int) inputHandler->selectedTile.y, 1); + IsometricMapChangeTextureIdOfTile(layers[n], (int) inputHandler->cursorWorldPos.x, (int) inputHandler->cursorWorldPos.y, 1); break; } } diff --git a/Input/inputHandler.h b/Input/inputHandler.h index aa82b38..e44b151 100644 --- a/Input/inputHandler.h +++ b/Input/inputHandler.h @@ -9,7 +9,7 @@ typedef struct InputHandler{ int pressed; Vector2 rectStart; Vector2 cursorPos; - Vector2 selectedTile; + Vector2 cursorWorldPos; int selectedLayer; } InputHandler; diff --git a/IsometricMap/isometricMap.c b/IsometricMap/isometricMap.c index 839f8e9..d3325f6 100644 --- a/IsometricMap/isometricMap.c +++ b/IsometricMap/isometricMap.c @@ -5,39 +5,37 @@ #include "raymath.h" #include "raylib.h" -IsometricMap * IsometricMapInit(int x, int y, int layer){ +IsometricMap * IsometricMapInit(int layer){ IsometricMap* map = (IsometricMap *) malloc(sizeof(IsometricMap)); map->tileTextures[0] = LoadTexture("assets/grass.png"); map->tileTextures[1] = LoadTexture("assets/tower.png"); map->originX = 0; map->originY = -layer * map->tileTextures[0].width / 4; - map->width = x * map->tileTextures[0].width; - map->height = y * map->tileTextures[0].height; + map->width = 100 * map->tileTextures[0].width; + map->height = 100 * map->tileTextures[0].height; map->widthBounds = 100; map->heightBounds = 100; + map->layer = layer; - Tile*** tiles = (Tile***)malloc(x*sizeof(Tile*)); + Tile*** tiles = (Tile***)malloc(map->widthBounds*sizeof(Tile*)); int n = 0; - for(n=0; nwidthBounds; n++){ + tiles[n] = (Tile**)malloc(map->heightBounds*sizeof(Tile*)); } - map->sizeX = x; - map->sizeY = y; - int i = 0; int j = 0; - - for(i=0; i < x; i++){ - for(j=0; j < y; j++){ + for(i=0; i < map->widthBounds; i++){ + for(j=0; j < map->heightBounds; j++){ Tile *tmp = (Tile *) malloc(sizeof(Tile)); - tmp->textureId = 0; + tmp->textureId = -1; tmp->x = i; tmp->y = j; tiles[i][j] = tmp; } } + map->tiles = tiles; return map; @@ -63,12 +61,13 @@ Tile * IsometricMapGetTileFromArrayPosition(IsometricMap *map, int x, int y){ void IsometricMapProject(IsometricMap *isometricMap, Camera2D *camera, float x, float y, Vector2 *tmp){ - int mouseAdjustmentX = -8; - int mouseAdjustmentY = -4; - float tileWidthHalf = isometricMap->tileTextures[0].width / 2; float tileHeightQuarter = isometricMap->tileTextures[0].height / 4; + int mouseAdjustmentX = -tileWidthHalf; + int mouseAdjustmentY = -tileHeightQuarter + (tileHeightQuarter * isometricMap->layer); + + x += camera->target.x + mouseAdjustmentX; y += camera->target.y + mouseAdjustmentY; @@ -92,11 +91,13 @@ Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap *isometricMap, float ptr->x = 0; ptr->y = 0; - if( x < isometricMap->sizeX && y < isometricMap->sizeY && + if( x < isometricMap->widthBounds && y < isometricMap->heightBounds && x >= 0 && y >= 0 ){ - ptr->x = isometricMap->tiles[(int)x][(int)y]->x; - ptr->y = isometricMap->tiles[(int)x][(int)y]->y; - return ptr; + if(isometricMap->tiles[(int)x][(int)y]->textureId != -1){ + ptr->x = isometricMap->tiles[(int)x][(int)y]->x; + ptr->y = isometricMap->tiles[(int)x][(int)y]->y; + return ptr; + } } ptr = 0; return ptr; @@ -105,23 +106,14 @@ Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap *isometricMap, float void IsometricMapAddTile(IsometricMap *isometricMap, int x, int y, int textureId){ - - //if(x < isometricMap->sizeX && y < isometricMap->sizeY){ - - Tile *tile = (Tile *) malloc(sizeof(Tile)); - tile->textureId = textureId; - tile->x = x; - tile->y = y; + isometricMap->tiles[x][y]->textureId = textureId; + isometricMap->tiles[x][y]->x = x; + isometricMap->tiles[x][y]->y = y; - //Tile tile = {textureId, x, y}; - isometricMap->tiles[x][y] = tile; - - //} - } void IsometricMapChangeTextureIdOfTile(IsometricMap *map, int x, int y, int id){ - if( x < map->sizeX && y < map->sizeY && + if( x < map->widthBounds && y < map->heightBounds && x >= 0 && y >= 0 ){ (map->tiles[x][y])->textureId = id; } diff --git a/IsometricMap/isometricMap.h b/IsometricMap/isometricMap.h index 1c08503..fdb2681 100644 --- a/IsometricMap/isometricMap.h +++ b/IsometricMap/isometricMap.h @@ -6,14 +6,13 @@ typedef struct IsometricMap{ Texture2D tileTextures[10]; Tile ***tiles; - int sizeX; - int sizeY; int originX; int originY; int width; int height; int widthBounds; int heightBounds; + int layer; } IsometricMap; // TODO: @@ -22,7 +21,7 @@ Tile * IsometricMapGetTileFromWorldCoordinates(IsometricMap *isometricMap, float Vector2 IsometricMapUnproject(IsometricMap *isometricMap, Camera2D *camera, int x, int y); // Working -IsometricMap * IsometricMapInit(int x, int y, int layer); +IsometricMap * IsometricMapInit(int layer); Vector2 * IsometricMapCalcOffsetForTileAt(int x, int y, int textureSize); Tile * IsometricMapGetTileFromArrayPosition(IsometricMap *isometricMap, int x, int y); // Screen Coordinates -> World Coordinates diff --git a/IsometricMap/isometricRenderer.c b/IsometricMap/isometricRenderer.c index a116fe1..46788c3 100644 --- a/IsometricMap/isometricRenderer.c +++ b/IsometricMap/isometricRenderer.c @@ -40,20 +40,22 @@ void IsometricRendererRenderIsometricMap(IsometricMap **map, InputHandler *input int n = 0; int i = 0; int j = 0; - for(n = 0; n < 9; n++){ + for(n = 0; n < 10; n++){ if(map[n] == 0){ break; } - for(i=0; i < map[n]->sizeX; i++){ - for(j=0; j < map[n]->sizeY; j++){ - Vector2 *offset = IsometricMapCalcOffsetForTileAt(i,j, map[n]->tileTextures[0].width); - - float x = map[n]->originX + offset->x; - float y = map[n]->originY + offset->y; - - int textureId = map[n]->tiles[i][j]->textureId; - - DrawTexture(map[n]->tileTextures[textureId], x, y, WHITE); + for(i=0; i < map[n]->widthBounds; i++){ + for(j=0; j < map[n]->heightBounds; j++){ + if(map[n]->tiles[i][j]->textureId != -1){ + Vector2 *offset = IsometricMapCalcOffsetForTileAt(i,j, map[n]->tileTextures[0].width); + + float x = map[n]->originX + offset->x; + float y = map[n]->originY + offset->y; + + int textureId = map[n]->tiles[i][j]->textureId; + + DrawTexture(map[n]->tileTextures[textureId], x, y, WHITE); + } } } } diff --git a/inputHandler.o b/inputHandler.o new file mode 100644 index 0000000..96e85f9 Binary files /dev/null and b/inputHandler.o differ diff --git a/isometricMap.o b/isometricMap.o new file mode 100644 index 0000000..4d645fb Binary files /dev/null and b/isometricMap.o differ diff --git a/isometricRenderer.o b/isometricRenderer.o new file mode 100644 index 0000000..c9cb721 Binary files /dev/null and b/isometricRenderer.o differ diff --git a/list.o b/list.o new file mode 100644 index 0000000..eb4318e Binary files /dev/null and b/list.o differ diff --git a/main.c b/main.c index 32a9a62..0f9f84d 100644 --- a/main.c +++ b/main.c @@ -27,8 +27,8 @@ int main(){ inputHandler.rectStart.y = 0; inputHandler.cursorPos.x = 0; inputHandler.cursorPos.y = 0; - inputHandler.selectedTile.x = 0; - inputHandler.selectedTile.y = 0; + inputHandler.cursorWorldPos.x = 0; + inputHandler.cursorWorldPos.y = 0; inputHandler.selectedLayer = -1; Camera2D camera = { 0 }; @@ -47,10 +47,47 @@ int main(){ IsometricMap **layers = (IsometricMap **) malloc(10*sizeof(IsometricMap *)); - layers[0] = IsometricMapInit(50, 80, 0); - layers[1] = IsometricMapInit(20, 20, 1); - layers[2] = IsometricMapInit(15, 10, 2); + int n = 0; + int i = 0; + int j = 0; + for(n = 0; n < 10; n++){ + layers[n] = IsometricMapInit(n); + } + + for(n = 0; n <= 10; n++){ + for(i = 0; i < 100; i++){ + for(j = 0; j < 100; j++){ + switch(n){ + case 0: + IsometricMapAddTile(layers[n], i, j, 0); + break; + case 1: + if(i > 35 && i < 50 && j > 45 && j < 60){ + IsometricMapAddTile(layers[n], i, j, 0); + } + break; + case 2: + if(i > 40 && i < 44 && j > 50 && j < 54){ + IsometricMapAddTile(layers[n], i, j, 1); + } + break; + } + + } + } + } + + for(n = 0; n <= 10; n++){ + for(i = 0; i < 20-n*2; i++){ + for(j = 0; j < 20-n*2; j++){ + IsometricMapAddTile(layers[n], i, j, 0); + if(n == 9){ + IsometricMapAddTile(layers[n], i, j, 1); + } + } + } + } SetTargetFPS(60); while(!WindowShouldClose()){ diff --git a/main.o b/main.o new file mode 100644 index 0000000..dfae523 Binary files /dev/null and b/main.o differ diff --git a/spiel b/spiel new file mode 100755 index 0000000..d142914 Binary files /dev/null and b/spiel differ diff --git a/sprite.o b/sprite.o new file mode 100644 index 0000000..31f9dba Binary files /dev/null and b/sprite.o differ diff --git a/tile.o b/tile.o new file mode 100644 index 0000000..ab55776 Binary files /dev/null and b/tile.o differ