你好,我对c/c ++语言有一些非常基本的了解,在15年前读了一本c和一本c ++书,大约一半.
大约在那个时候,在1998年至9月,我买了一本书"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");
}
Run Code Online (Sandbox Code Playgroud)
书中的代码,如上所述,除了注释的无符号行是原始的,但是未注释的行被修改.更改无符号行后,它将使用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);
}
Run Code Online (Sandbox Code Playgroud)
它现在工作正常,没有分段错误.谢谢.
far是一个非标准的C关键字.除非你确定需要它,否则忘了它.
从代码中,它是一个简单的猜数游戏,clock应该为随机生成器提供种子.0x0000046CL又是一些导致分段错误的非可移植代码.
对于简单用途,请使用当前时间作为种子.
srand(time(0))
Run Code Online (Sandbox Code Playgroud)