我正在阅读有关如何在C中实现某些面向对象功能的内容,并且它已被证明非常有用.具体来说,我一直在玩弄继承的想法.这是一个例子:
typedef struct Circle{
int rad, x, y;
//Other things...
} Circle;
typedef struct Entity{
Circle body;
//Entity-specific items...
} Entity;
Run Code Online (Sandbox Code Playgroud)
这很简单,但它允许偷偷摸摸的东西.指向实体的指针也是指向Circle的指针,因为实体的第一个元素始终是Circle.有了这个想法,我们可以构造以下功能:
int checkCircleCollision(Circle* one, Circle* two);
Run Code Online (Sandbox Code Playgroud)
并称之为:
Entity* myentity = createEntity(/* Things specific to my entity */);
Entity* myotherentity = createEntity(/* Different things */);
//Did they collide?
if (checkCircleCollision(myentity, myotherentity)){
/* ... */
}
Run Code Online (Sandbox Code Playgroud)
这太棒了,但我遇到了一个问题.如果我想让我的某些实体成为矩形怎么办?我有一个解决方案,但无论编译器如何,我都希望确认它始终有效.我对工会的了解非常有限.
//Circle defined as above...
typedef struct Rectangle{
int x, y, w, h;
//Other things...
} Rectangle;
int checkRectangleCollision(Rectangle* one, Rectangle* two);
int checkRectangleCircleCollision(Rectangle* rect, …Run Code Online (Sandbox Code Playgroud) 我目前正在使用SDL2 Library和C编写一个iPhone应用程序,而且大部分内容都很顺利.不幸的是,文档似乎在某些方面相当薄,特别是iOS特定的功能.我是使用SDL2的新手,这让事情变得非常困难.到目前为止,一切都有效,但我对一个问题感到难过.SDL2定义了六种专门用于移动应用程序的事件类型.README-ios.txt文件描述了它们并使用它们:
int HandleAppEvents(void *userdata, SDL_Event *event)
{
switch (event->type)
{
case SDL_APP_TERMINATING:
/* Terminate the app.
Shut everything down before returning from this function.
*/
return 0;
case SDL_APP_LOWMEMORY:
/* You will get this when your app is paused and iOS wants more memory.
Release as much memory as possible.
*/
return 0;
case SDL_APP_WILLENTERBACKGROUND:
/* Prepare your app to go into the background. Stop loops, etc.
This gets called when the user hits the home button, or gets …Run Code Online (Sandbox Code Playgroud)