您好,在15年前读了一本c和一本c ++书后,我对c / c ++语言有一些非常基本的了解。
大约在那个时候,在1998-9ish,我买了一本书“ 3D游戏编程的妖术,用c编写自己的高速3d多边形视频游戏”。出版商-韦特(Waite),作家-安德烈(Andre LaMothe)。它的位置很容易,因此您不必了解c就可以成为学习本书的先决条件。那时候我真的很想学习它,但是却参与了其他事情,并被其他项目分散了注意力。大约在那个时候,我发现了其他几种语言。我尝试过perl,真的很喜欢它。我很快就了解了它的基础知识,并用3个月+ 3个月的时间编写了我的第一个大型项目,并用perl对其进行了修复和微调。从那时起,我开始学习更多知识并改进perl,因此没有时间来学习c。
学习游戏和图形编程的愿望从未离开过我,因此,我决定回到那本游戏编程书中。我知道,您现在可以学习OpenGL或WebGL,但是在我看来,这本书有很多低层次的概念,如果您不学习,将不会像可能的那样成为游戏编程的好手,但是,本书要求您具有MS C / C ++ 7.0编译器。从那时起,我已经转移到linux(5年前)了,不想回到Windows。而且,无论我学到什么,我都希望它是跨平台的,所以我宁愿弄清楚如何修改本书的代码以在gcc中进行编译,然后在wine或Windows虚拟机中安装MS C / C ++ 7.0编译器。
在书里:
//input driven event loops
//includes
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void main(void){
int done=0,
number,
num_tries=0,
guess;
//removed far from the unsigned line, because compiler complains:
//error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
// unsigned int far *clock = (unsigned int far *)0x0000046CL; //clock pointer
unsigned int *clock = (unsigned int *)0x0000046CL; //clock pointer
//section 1
printf("\nI'm thinking of a number from 1-100.");
printf("\nTry and guess it!\n");
srand(*clock);
number = 1 + rand() % 100;
printf("test");
}
本书中的代码同上,不同之处在于注释的无符号行是原始行,但未注释的行被修改。更改无符号行后,它将使用gcc file_name.c进行编译,但是当编译后的二进制文件的执行到达srand行时,该程序将因分段错误而退出。我想,“远”事与ms编译器有关,也许整行都与获取时钟指针有关。关于如何解决该问题的任何建议?
好的,我知道时钟指针行在现代编程中是无用的,我应该使用time.h,因此我将时钟指针更改为time()函数。另外,如建议的那样,我已经将return和int添加到了main函数中。这是新的代码:
//input driven event loops
//includes
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int main(void){
int done=0,
number,
num_tries=0,
guess;
//section 1
printf("\nI'm thinking of a number from 1-100.");
printf("\nTry and guess it!\n");
srand(time(NULL));
number = 1 + rand() % 100;
return(0);
}
现在工作正常,没有分段错误。谢谢你。
far
是非标准的C关键字。除非您确定需要它,否则请不要理会它。
从代码来看,这是一个简单的猜测数字游戏,clock
应该为随机数生成器提供种子。0x0000046CL
再次,是一些导致分段错误的不可移植代码。
为了简单起见,请使用当前时间作为种子。
srand(time(0))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句