0

I keep getting the error "func.h:23: error: cannot cast 'int' to 'struct Texture'" even though im inputting text as tile in renderTiles(). Am i just being dumb here? ¯_(ツ)_/¯ Im new to C so this might just be me. i copied and pasted the basic window example to create this. i know there isnt char before tile in "renderTiles(tile)" i tried that as well and it still did not work

main.c:

*
*   raylib [core] example - Basic window
*
*   Welcome to raylib!
*
*   To test examples, just press F6 and execute raylib_compile_execute script
*   Note that compiled executable is placed in the same folder as .c file
*
*   You can find all basic examples on C:\raylib\raylib\examples folder or
*   raylib official webpage: www.raylib.com
*
*   Enjoy using raylib. :)
*
*   Example originally created with raylib 1.0, last time updated with raylib 1.0

*   Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
*   BSD-like license that allows static linking with closed source software
*
*   Copyright (c) 2013-2022 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#include "raylib.h"
#include "func.h"

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
    // Initialization
    //--------------------------------------------------------------------------------------
    const int screenWidth = 768;
    const int screenHeight = 576;

    InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");

    SetTargetFPS(60);               // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------
    // textures
    Texture2D grass = LoadTexture("grass.png");
    Texture2D stone = LoadTexture("stone.png");
    Texture2D sand = LoadTexture("sand.png");
    Texture2D stone_oasis = LoadTexture("stone-oasis.png");
    Texture2D sand_oasis = LoadTexture("sand-oasis.png");
    Texture2D UI = LoadTexture("ui.png");
    
    // Main game loop
    while (!WindowShouldClose())    // Detect window close button or ESC key
    {
        // Update
        //----------------------------------------------------------------------------------
        // TODO: Update your variables here
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();
            
            ClearBackground(RAYWHITE);
            DrawTexture(UI, 0, 0, RAYWHITE);
            renderTiles("grass");
            
        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    CloseWindow();        // Close window and OpenGL context
    //--------------------------------------------------------------------------------------
}

func.h

#ifndef FUNC
#define FUNC

const int screenWidth = 768;
const int screenHeight = 576;

int centerX(int x)
{
    int ox = x + (screenWidth / 2);
    return (ox);
}

int centerY(int y)
{
   int oy = y + (screenHeight / 2);
   return(oy);
}
void renderTiles(tile)
{
   for( int b = 0; b < 12; b = b + 1)
      {
         for( int a = 0; a < 12; a = a + 1 ){
               DrawTexture(tile, (a * 48) + 96, (b * 48), RAYWHITE);

            }
      }
}

#endif
justtab
  • 23
  • 4
  • You forgot to add a type for `tile`, which makes it default to `int`. Changing to `void renderTiles(const char* tile)` should clear it up. Compiling with warnings and not ignoring them also helps in general. – masaers Feb 01 '23 at 00:34

1 Answers1

1

Answering the question title, you have already converted the string to a texture struct with

Texture2D grass = LoadTexture("grass.png");

The raylib cheat sheet shows the function you are calling to be

void DrawTexture(Texture2D texture, int posX, int posY, Color tint);

but you have passed the char* value "grass" to your function, which has an untyped argument, so the compiler assumes it to be type int.

Your function should be

/* void renderTiles(tile) */
void renderTiles(Texture2D tile)

and you should call it with

/* renderTiles("grass"); */
renderTiles(grass);

Also, you should not have executable code in a header file.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56