Raylib中char转struct Texture方法及“int转struct Texture”编译报错求解
问题:C语言Raylib编译错误:无法将'int'转换为'struct Texture'
我是C语言新手,基于Raylib基础窗口示例编写代码时,一直出现编译错误:func.h:23: error: cannot cast 'int' to 'struct Texture'。调用renderTiles()时传入字符串作为tile参数,尝试在参数前添加char修饰后问题仍未解决,请问如何实现char到struct Texture的转换并解决该报错?
相关代码如下:
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
解决方案
错误根源
renderTiles函数未声明参数类型,C语言会默认将无类型参数视为int,但你传入的是字符串字面量(const char*),后续DrawTexture需要的是Texture2D类型,直接导致类型不匹配报错。- 字符串是纹理的文件名标识,
Texture2D是加载到内存的纹理对象,两者不能直接转换,你的思路从根源上有误。
修复步骤
1. 修正renderTiles的参数类型
直接把renderTiles的参数改成Texture2D类型,因为DrawTexture需要的就是这个类型:
修改func.h中的函数定义:
void renderTiles(Texture2D 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); } } }
2. 修改调用方式,传入已加载的纹理对象
在main.c的渲染部分,把renderTiles("grass")改成直接传入已经加载好的grass变量:
renderTiles(grass);
可选:通过字符串名称选择纹理
如果确实需要用字符串来指定要渲染的纹理,可以用字符串匹配实现(记得包含<string.h>头文件):
在main.c中添加辅助函数:
#include <string.h> Texture2D getTextureByName(const char* name) { if (strcmp(name, "grass") == 0) return grass; if (strcmp(name, "stone") == 0) return stone; if (strcmp(name, "sand") == 0) return sand; // 其他纹理的匹配逻辑 return (Texture2D){0}; // 返回空纹理作为默认值 }
调用时:
renderTiles(getTextureByName("grass"));
额外修正:解决重复定义问题
func.h和main.c中都定义了screenWidth和screenHeight,会导致重复定义警告,建议只在一处定义:
在func.h中修改为外部声明:
extern const int screenWidth; extern const int screenHeight;
保留main.c中的定义即可。
内容的提问来源于stack exchange,提问作者justtab
相关产品推荐
相关产品推荐

