我觉得我在c ++中误解了一个大概念 (不能买任何书。) 我无法在此找到任何其他内容,如果隐藏在互联网不明显部分的内容出现,请对不起。 当我找到一种为sdl创建应用程序包装器的新方法时,我最近一直在尝试使用SDL。但是当把它分成一个单独的cpp和头文件时......就会发生一些事情。
C ++:
//
// AppSdl.cpp
// sdlgaim
//
// Created by Home on 28/4/14.
// Copyright (c) 2014 hyperum. All rights reserved.
//
#include "AppSdl.h"
app_sdl::app_sdl() :
_running(false)
{
}
app_sdl::~app_sdl()
{
destroy();
}
int app_sdl::init(int width, int height, const char *title)
{
// Initialize the SDL library.
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
{
fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
return APP_FAILED;
}
win = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
// Success.
return APP_OK;
}
void app_sdl::destroy()
{
if (win)
{
SDL_DestroyWindow(win);
SDL_DestroyRenderer(renderer);
SDL_Quit();
}
}
int app_sdl::run(int width, int height, const char *title)
{
// Initialize application.
int state = init(width, height, title);
if (state != APP_OK) return state;
// Enter to the SDL event loop.
SDL_Event ev;
_running = true;
while (SDL_WaitEvent(&ev))
{
onEvent(&ev);
Render();
if (_running == false)
{
break;
}
}
// Success.
return APP_OK;
}
void app_sdl::onEvent(SDL_Event* ev)
{
switch (ev->type)
{
case SDL_QUIT:
_running = false;
break;
case SDL_KEYDOWN:
{
switch (ev->key.keysym.sym)
{
case SDLK_ESCAPE:
_running = false;
break;
}
}
}
}
void app_sdl::Render()
{
SDL_Rect r;
int w,h;
SDL_GetWindowSize(win, &w, &h);
r.w = 200;
r.h = 200;
r.x = w/2-(r.w/2);
r.y = h/2-(r.h/2);
//
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0, 0xff);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0, 0xff);
SDL_RenderFillRect(renderer, &r);
SDL_RenderPresent(renderer);
}
部首:
//
// AppSdl.h
// sdlgaim
//
// Created by Home on 28/4/14.
// Copyright (c) 2014 hyperum. All rights reserved.
//
#ifndef __sdlgaim__AppSdl__
#define __sdlgaim__AppSdl__
#include <iostream>
#include <SDL2/SDL.h>
struct app_sdl
{
app_sdl();
~app_sdl();
// Application state (just convenience instead of 0, 1, ...).
enum APP_STATE
{
APP_OK = 0,
APP_FAILED = 1
};
// Destroy application, called by destructor, don't call manually.
void destroy();
int init(int width, int height, const char *title);
// Run application, called by your code.
int run(int width, int height, const char *title);
// Called to process SDL event.
void onEvent(SDL_Event* ev);
// Called to render content into buffer.
void Render();
// Whether the application is in event loop.
bool _running;
SDL_Window *win;
SDL_Renderer *renderer;
};
#endif /* defined(__sdlgaim__AppSdl__) */
现在,您看到,当我包含cpp文件并执行此操作时: {@ 1}}在我的主整数中,一切运行正常。但是当我包含HEADER文件时,会发生这种情况:
app_sdl app;
return app.run(640, 480, APPTITLE);
任何人都知道这里发生了什么?我该怎么做?
答案 0 :(得分:4)
正如您所知,包括cpp文件在内,即使它似乎解决了这个问题,也不是正确的做法。
当您忘记将源文件添加到编译命令时,就会发生错误。
我从未使用过xcode,但快速的Google搜索结果显示this page表示:
- 检查缺少哪些符号
- target-&gt;构建阶段 - &gt;编译源
- 添加缺少的源文件(如果未列出)
- command + b再次编译源代码
醇>