我一直在尝试在控制台中发送日志以确保播放器已创建,但结果如下:
以下是问题部分的代码:
// Core Libraries and Headers
// - Global includes
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
#include <typeinfo>
// Defines
#define GRAPHICS "../resources/"
struct Player
{
std::string sprite;
std::string name;
int level;
int size;
int health;
int maxhealth;
int magicpower; // Magic Points
int damages;
int armor;
int magic;
};
struct Enemy
{
std::string sprite;
std::string name;
int level;
int size;
int health;
int maxhealth;
int magicpower; // Magic Points
int damages;
int armor;
int magic;
};
Player createPlayer(std::string name);
Enemy createEnemy(std::string name, int level);
int damagePlayer(Player target, int damages);
int damageEnemy(Enemy target, int damages);
void close();
systemValues.h:
// Local includes
// - Custom Headers
#include "systemValues.h"
// Screen constants and dimensions. (TODO: MOVE TO A PROPER HEADER FILE.)
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
// The window that is rendered to
SDL_Window* window = NULL;
// Quit being false means the application is running and did not request to quit
bool quit = false;
// Event handler
SDL_Event e;
// The surface contained by the window
SDL_Surface* screenSurface = NULL;
// Main function
int main(int argc, char* args[])
{
// Initializing SDL
if (SDL_Init(SDL_INIT_VIDEO)<0)
{
SDL_Log("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
}
else
{
// Creating the window
window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL)
{
SDL_Log("The window could not be created! SDL_Error: %s\n", SDL_GetError());
}
else
{
Player player1 = createPlayer("Avarthar");
Enemy enemy1 = createEnemy("Blob", 1);
// Get the window surface
screenSurface = SDL_GetWindowSurface(window);
// While the application is running
while (!quit){
while (SDL_PollEvent(&e) != 0)
{
// User requests quit
if (e.type == SDL_QUIT)
{
quit = true;
}
// Checks for a key being pressed down
else if (e.type == SDL_KEYDOWN)
{
switch(e.key.keysym.sym)
{
// Checks if the key is F
case SDLK_f:
player1.health = damagePlayer(player1, 5);
SDL_Log("%s has %i hp out of %i", player1.name, player1.health, player1.maxhealth); // TODO - Remove the test line
break;
case SDLK_ESCAPE:
quit = true;
break;
}
}
}
// Test if player is created
if (player1.name == "Avarthar")
{
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, (player1.health*-1+100*2), player1.health*2, 0));
}
else
{
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 255, 255, 255));
}
// Update the surface
SDL_UpdateWindowSurface(window);
}
}
}
close();
// Return 0 to terminate the program
return 0;
}
// Creates a player with the given varables
Player createPlayer(std::string name)
{
Player player;
player.sprite = "";
player.name = name;
player.level = 1;
player.size = 32;
player.health = 100;
player.maxhealth = 100;
player.magicpower = 100;
player.damages = 1;
player.armor = 0;
player.magic = 1;
return player;
}
Enemy createEnemy(std::string name, int level)
{
Enemy enemy;
enemy.sprite = "";
enemy.name = name;
enemy.level = level;
enemy.size = 32;
enemy.health = 100;
enemy.maxhealth = 100;
enemy.magicpower = 100; // Magic Points
enemy.damages = 1;
enemy.armor = 0;
enemy.magic = 1;
return enemy;
}
// Deals damage to the target of type Player
int damagePlayer(Player target, int damages)
{
target.health -= damages;
// Checks whether the target is in the health range or not
if (target.health < 0)
target.health = 0;
if (target.health > target.maxhealth)
target.health = target.maxhealth;
return target.health;
}
// Deals damage to the target of type Enemy
int damageEnemy(Enemy target, int damages)
{
target.health -= damages;
// Checks whether the target is in the health range or not
if (target.health < 0)
target.health = 0;
if (target.health > target.maxhealth)
target.health = target.maxhealth;
return target.health;
}
void close()
{
// Wait 0.1 seconds
SDL_Delay(100);
// Clear the window from the memory
SDL_DestroyWindow(window);
// Quit the SDL subsystems
SDL_Quit();
}
main.cpp中:
&player1.name
到目前为止我尝试了什么:
SDL_Log("%s", player1.name);
并没有改变任何内容。?
,而是在输出中写了一个player1.health
。根据SDL_Log
更改颜色的工作没有任何问题,但是当我尝试在malloc(sizeof(10))
中获取并发送名称时,它会发送上面屏幕截图中显示的输出。
答案 0 :(得分:1)
SDL_Log
无法std::string
。使用player1.name.c_str()
。这应该已经触发了警告,检查你的编译器标志。 - 昆汀
感谢您的快速回答,也感谢您注意到我的编译器存在的另一个问题^^