void S_Init() { // Starta OpenAL device = alcOpenDevice(NULL); if (device == NULL) { SYS_Error("Failed to open an audio device!\n"); } ALCcontext* context = alcCreateContext(device, NULL); if (context == NULL) { SYS_Error("Failed to initialize OpenAL!\n"); } alcMakeContextCurrent(context); S_rockets = S_LoadWav("res/Rockets.wav"); }
SYS_SOUNDHANDLE SYS_LoadSound(char* filename) { int len = strlen(filename); char ext[4]; ext[0] = tolower(filename[len-3]); ext[1] = tolower(filename[len-2]); ext[2] = tolower(filename[len-1]); ext[3] = 0; SYS_SOUNDHANDLE result; if (strcmp("wav", ext) == 0) { result = SYS_LoadWAVFile(filename); } else if (strcmp("mp3", ext) == 0) { result = SYS_LoadMP3File(filename); } else { SYS_Error("Could not open %s\n", filename); result = 0; } #ifdef SYS_SOUNDDEBUG SYS_Wave* wave = (SYS_Wave*)result; fprintf( soundLog, "SYS_LoadSound( \"%s\" ), %d loaded, %d free\n", filename, soundsLoaded, soundsFree ); fprintf(soundLog, " buffer = %d, source = %d, mp3_buffers = %d, %d\n", wave->buffer, wave->source, wave->mp3_buffers[0], wave->mp3_buffers[1]); #endif return result; }
static SYS_Wave* GetWaveFromFreePool() { SYS_Wave* wave; if ( freeList == 0 ) SYS_Error( "AllocWaveFromPool() out of waves!\n" ); // grab a wave off of the free list wave = freeList; freeList = wave->next; if ( freeList ) freeList->prev = 0; // and stick it in the used list wave->next = usedList; if ( usedList ) usedList->prev = wave; usedList = wave; // init the resources. wave->buffer = 0; wave->source = 0; wave->mp3_buffers[0] = 0; wave->mp3_buffers[1] = 0; wave->filename = 0; wave->mh = 0; #ifdef SYS_SOUNDDEBUG soundsLoaded++; soundsFree--; #endif return wave; }
void SCH_Init() { l_head = NULL; l_listSize = 0; MTX_Create(&l_mtx); COND_Create(&l_cond); // spawn scheduler l_exit = 0; if(THR_Create(&l_thr, Scheduler, NULL)) SYS_Error("SCH_Init: Failed to spawn scheduler thread!"); }
void KeyDispose() { //翻盖处理跟检测 if(BoxOpenCheck()) return ; //USB状态检测 //USbInCheck() ; // USB信息处理 UsbInfoDispose(); // 按键信息处理 KeyInfoDispose(); //定时器锁定信息 #ifdef DS1307_ON if(System.Sys.PassWrd.InputErrCount >= INPUT_ERR_TIME_MAX) { uint32_t m ; GR_DBG_PRINTF("System.Sys.PassWrd.InputErrCount: %d \n\n ",System.Sys.PassWrd.InputErrCount); GR_DBG_PRINTF("System.Sys.PassWrd.LockTime: %d \n\n ",System.Sys.PassWrd.LockTime); DS1307_RD(&date[0]); m = sTime(NowTime); GR_DBG_PRINTF("sTime(NowTime): %d \n\n",m); m = m - System.Sys.PassWrd.LockTime; GR_DBG_PRINTF("locked time is gone: %d \n\n",m); if ( m >= SYS_LOCK_TIME_SETTING ) //锁定时间2分钟 { System.Sys.PassWrd.InputErrCount = 0 ; System.Sys.SaveInfoFlag = 0x01 ; } } #else if(System.Sys.PassWrd.InputErrCount >= INPUT_ERR_TIME_MAX) { System.Sys.PassWrd.InputErrCount = 0 ; System.Sys.SaveInfoFlag = 0x01 ; } #endif // 系统保存信息 if(System.Sys.SaveInfoFlag == 0x01) { SystemWriteBCKData(); } // 系统软硬件错误 SYS_Error(); }
void PlayFliEnter( const char* filename ) { char newfilename[512]; sprintf( newfilename, "%s%s", g_DataPath, filename ); // open the fli gCurrentFile = fopen( newfilename, "rb" ); if ( !gCurrentFile ) { SYS_Error( "could not open FLI file!\n" ); return; } fread( &gCurrentHeader, sizeof( flic_header ), 1, gCurrentFile ); /* Get the flic header */ fseek( gCurrentFile, 128, SEEK_SET ); /* Move to end of header */ DB_Clear_Screen(); /* Clear the double buffer */ gFrameTime = SYS_GetTimeMS(); }
void Load_All_AI(Player* players, const char* filename, int* ai_indices) { // append path to front of filename. char newfilename[512]; sprintf(newfilename, "%s%s",g_DataPath, filename); FILE* fp = fopen(newfilename, "r"); if (!fp) SYS_Error("Error loading AI\n"); // pre allocate the maximum number of characters character_type* ais = (character_type*)malloc(sizeof(character_type) * MAX_AIS); memset(ais, 0, sizeof(character_type) * MAX_AIS); // open the YAML file yaml_parser_t parser; yaml_parser_initialize(&parser); yaml_parser_set_input_file(&parser, fp); // NOTE: YAML file is expected to contain a sequence of maps. // One map per character. int done = 0; int ai_index = 0; char key[128]; bool read_key = false; while (!done) { yaml_event_t event; // get the next event if (!yaml_parser_parse(&parser, &event)) { SYS_Error("error parsing YAML file \"%s\"\n", newfilename); return; } // process the event switch (event.type) { case YAML_MAPPING_START_EVENT: read_key = true; break; case YAML_SCALAR_EVENT: if (read_key) strcpy(key, (char*)event.data.scalar.value); else Character_Set(&ais[ai_index], key, (char*)event.data.scalar.value); read_key = !read_key; break; case YAML_MAPPING_END_EVENT: assert(ai_index < MAX_AIS); // too many ais in yaml file. ai_index++; break; case YAML_NO_EVENT: case YAML_STREAM_START_EVENT: case YAML_STREAM_END_EVENT: case YAML_DOCUMENT_START_EVENT: case YAML_DOCUMENT_END_EVENT: case YAML_ALIAS_EVENT: case YAML_SEQUENCE_START_EVENT: case YAML_SEQUENCE_END_EVENT: default: break; } done = (event.type == YAML_STREAM_END_EVENT); yaml_event_delete(&event); } yaml_parser_delete(&parser); // load each player's sound fx. for (int i = 0; i < 6; ++i) { character_type* c = &players[i].character; memcpy(c, &ais[ai_indices[i]], sizeof(character_type)); for (int j = 0; j < NUMBER_CHARACTER_SOUNDS; ++j) c->samples[j] = SYS_LoadSound(c->sample_filenames[j]); } free(ais); }
void V_CreateHeightMap(model_t* m, sprite* s, double size, double height) { int w = s->w, h = s->h; if (w < 1 || h < 1) SYS_Error("Heightmap has an invalid width or height. "); int vertCount = w * h; int indexCount = (w - 1) * (h - 1) * 6; memset(m, 0, sizeof(model_t)); for (int i = 0; i < m->boneCount; i++) m->bones[i].keyCount = 0; VAO_t* vao = &m->vao; glGenVertexArrays(1, &vao->id); glBindVertexArray(vao->id); vao->vertCount = vertCount; vao->indexCount = indexCount; vao->vert.bufferSize = 3 * vertCount * sizeof(float); vao->vert.buffer = malloc(vao->vert.bufferSize); double hScale = size / w, vScale = size / h; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { double sampleHeight = ((s->pix[y * w + x] & 0xFF000000) >> 24) / 256.0; ((float*)vao->vert.buffer)[y * w * 3 + x * 3 + 0] = x * hScale; ((float*)vao->vert.buffer)[y * w * 3 + x * 3 + 1] = sampleHeight * height; ((float*)vao->vert.buffer)[y * w * 3 + x * 3 + 2] = y * vScale; } } V_InitVBO(&vao->vert, 0, 3, GL_FLOAT); vao->uv.bufferSize = 3 * vertCount * sizeof(float); vao->uv.buffer = malloc(vao->uv.bufferSize); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { ((float*)vao->uv.buffer)[y * w * 3 + x * 3 + 0] = x; ((float*)vao->uv.buffer)[y * w * 3 + x * 3 + 1] = y; ((float*)vao->uv.buffer)[y * w * 3 + x * 3 + 2] = 0; } } V_InitVBO(&vao->uv, 1, 3, GL_FLOAT); vao->normal.bufferSize = 3 * vertCount * sizeof(float); vao->normal.buffer = malloc(vao->normal.bufferSize); for (int i = 0; i < vertCount; i++) { vec3 normal = {0, 0, 0}; int x = i % w, y = i / w; int xMin = x == 0 ? x : x - 1; int xMax = x >= w - 1 ? x : x + 1; float yDelta = ((float*)vao->vert.buffer)[y * w * 3 + xMax * 3 + 1] - ((float*)vao->vert.buffer)[y * w * 3 + xMin * 3 + 1]; float xDelta = xMax - xMin; float zAngle = atan2(yDelta, xDelta); normal[1] = cos(zAngle); int yMin = y == 0 ? y : y - 1; int yMax = y >= h - 1 ? y : y + 1; yDelta = ((float*)vao->vert.buffer)[yMax * w * 3 + x * 3 + 1] - ((float*)vao->vert.buffer)[yMin * w * 3 + x * 3 + 1]; float zDelta = yMax - yMin; float xAngle = atan2(yDelta, zDelta); normal[1] *= cos(xAngle); normal[0] = -sin(zAngle); normal[2] = -sin(xAngle); ((float*)vao->normal.buffer)[i * 3 + 0] = normal[0]; ((float*)vao->normal.buffer)[i * 3 + 1] = normal[1]; ((float*)vao->normal.buffer)[i * 3 + 2] = normal[2]; } V_InitVBO(&vao->normal, 2, 3, GL_FLOAT); vao->tangents.bufferSize = 3 * vertCount * sizeof(float); vao->tangents.buffer = malloc(vao->tangents.bufferSize); for (int i = 0; i < vertCount; i++) { vec3_mul_cross(&((float*)vao->tangents.buffer)[i * 3], &((float*)vao->normal.buffer)[i * 3], (vec3) {0, 0, 1}); } V_InitVBO(&vao->tangents, 4, 3, GL_FLOAT); glGenBuffers(1, &vao->index.id); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vao->index.id); vao->index.type = GL_UNSIGNED_INT; vao->index.dim = 1; vao->index.buffer = malloc(indexCount * sizeof(int)); for (int x = 0; x < w - 1; x++) { for (int y = 0; y < h - 1; y++) { ((int*)vao->index.buffer)[y * (w - 1) * 6 + x * 6 + 0] = y * w + x; ((int*)vao->index.buffer)[y * (w - 1) * 6 + x * 6 + 1] = (y + 1) * w + x; ((int*)vao->index.buffer)[y * (w - 1) * 6 + x * 6 + 2] = y * w + x + 1; ((int*)vao->index.buffer)[y * (w - 1) * 6 + x * 6 + 3] = (y + 1) * w + x; ((int*)vao->index.buffer)[y * (w - 1) * 6 + x * 6 + 4] = (y + 1) * w + x + 1; ((int*)vao->index.buffer)[y * (w - 1) * 6 + x * 6 + 5] = y * w + x + 1; } } glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount * sizeof(int), vao->index.buffer, GL_STATIC_DRAW); vao->weights.buffer = NULL; }
/* Function PCX load loads and uncompresses a PCX image into image->buffer And loads the palette into image->palette Based on code from Graphics Gurus book */ void PCX_Load( char *filename, pcx_picture_ptr image ) { FILE *fp; /* File pointer */ long index; /* Multipurpose index */ long new_pos = 0; /* Position in the image->buffer */ int num_bytes; /* Number of bytes in current RLE run */ int info; /* Data from temp_buffer */ unsigned char *p; int scan_line_length; char newfilename[512]; sprintf(newfilename,"%s%s",g_DataPath,filename); if ( (fp = fopen( newfilename, "rb") ) == NULL ) /* Open the PCX file */ { SYS_Error("ERROR OPENING FILE!!! %s\n", newfilename); exit(0); } index = 0; /* Read in the header */ fseek( fp, 0, SEEK_SET ); /* Move to beginning of file */ fread( &image->header, sizeof( pcx_header ), 1, fp ); /* Compute the width of the image in pixels */ image->xpixels = image->header.xend - image->header.xstart; /* Length of image in pixels */ image->ypixels = image->header.yend - image->header.ystart; /* Figure out how many pixels per line */ scan_line_length = ( image->header.bytes_per_line * image->header.num_bit_planes ); image->xpixels = scan_line_length - 1; /* Allocate memory for picture...the +100 is just to be safe */ image->buffer = (unsigned char *)malloc( ((long)(image->xpixels + 1 ) * (long)(image->ypixels + 1)) + 100 ); if ( image->buffer == NULL ) { printf("Not Enough Memory!!!"); exit(0); } p = image->buffer; /* Decompress data from file into image->buffer */ fseek( fp, 128, SEEK_SET ); /* Move to 128 bytes from beginning of file */ while( new_pos < ( ((long)image->xpixels + 1 ) * (long)(image->ypixels + 1) ) ) /* Loop till beginning of color palette */ { /* And end of data */ info = getc(fp); /* Store one char from the buffer */ /* In info */ if ( info >= 192 && info <= 255 ) /* Test to se if the value in */ { /* Info is between 192 and 255 */ num_bytes = info - 192; /* If it is then get the next */ /* Get actual data for the run Value from the buffer and */ info = getc(fp); /* Replicate it num_bytes times In the new buffer */ /* Replicate the data in the buffer num_bytes times */ while( num_bytes-- > 0 ) { p[new_pos++] = info; } /* End while */ } /* End if RLE */ else /* If the info value wasn't between 192 and 255 */ { /* Then just replicate it into the new buffer */ p[new_pos++] = info; } /* End else if not RLE */ } /* End while */ image->num_bytes = new_pos; /* This is the number of bytes in the uncompressed image */ fseek( fp, -768L, SEEK_END ); /* Move to beginning of color palette */ /* Which is 768 bytes before the end */ /* Load color palette */ for (index = 0; index < 256; index ++) { image->palette[index].red = getc(fp) >>2; image->palette[index].green = getc(fp) >>2; image->palette[index].blue = getc(fp) >>2; } /* End for index */ /* Color palette loaded */ fclose( fp ); /* Close the PCX file */ } /* End of pcx load */
int main(int argc, char** argv) { SYS_argc = argc; SYS_argv = argv; // Check command line options if (SYS_HasParam("-l")) { if (argc != 3) { PrintUsage(); return 0; } // Load the level and print its data L_LoadLevel(argv[argc - 1]); printf("Name: %s\n", L_current.name); printf("Contains %d resources and %d props\n", L_current.res.size, L_current.props.size); // Print name of every resource printf("\nResources\n"); for (int i = 0; i < L_current.res.size; i++) printf("\t%d:\t%s\n", i, ((resource*)ListGet(&L_current.res, i))->name); // Print data of every prop printf("\nProps\n"); for (int i = 0; i < L_current.props.size; i++) { prop* p = ListGet(&L_current.props, i); printf( "\t%d:\tResource: %s\n\t\tPos: %f, %f, %f\n\t\tRot: %f, %f, %f\n", i, p->res->name, p->pos[0], p->pos[1], p->pos[2], p->rot[0], p->rot[1], p->rot[2] ); } return 0; } // Initialize console variables C_Init(); // Try to open a window with rendering capabilities if (!SYS_OpenWindow()) SYS_Error("Failed to open a window with OpenGL Core 3.3"); // Initialize OpenGL V_InitOpenGL(); fflush(stdout); // Initialize main engine G_Init(); fflush(stdout); // Initialize rendering system V_Init(); fflush(stdout); // Main game loop uint32_t current = SYS_TimeMillis(); uint32_t last = current; uint32_t secondTimer = 0, frames = 0; while (!SYS_WindowClosed()) { last = current; current = SYS_TimeMillis(); SYS_deltaMillis = current - last; secondTimer += SYS_deltaMillis; frames++; if (secondTimer >= 1000) { secondTimer -= 1000; printf("\rFPS: %d", frames); fflush(stdout); frames = 0; } // Update game, then render to screen G_Tick(); V_Tick(); SYS_UpdateWindow(); } printf("\n"); G_Quit(); V_Quit(); return 0; }
// Ladda en wav ljud-fil Clip S_LoadWav(const char* link) { // Öppna filen char path[PATH_LENGTH] = {0}; strcpy(path, SYS_GetBasePath()); strcpy(path, "/"); strcpy(path, link); FILE* file = fopen(path, "rb"); if (!file) { SYS_Error("Couldn't open file! \n"); return (Clip) {0}; } Clip c; // Läs headern unsigned char riff[12]; for (int i = 0; i < 12; i++) riff[i] = fgetc(file); // Kolla att filen verkligen är en .wav fil if (memcmp(riff, "RIFF", 4) != 0) { SYS_Error("Sound file is not a .wav file\n"); fclose(file); return (Clip) {0}; } if (memcmp(&riff[8], "WAVE", 4) != 0) { SYS_Error("Sound file is not a .wav file\n"); fclose(file); return (Clip) {0}; } unsigned char fmt[24]; for (int i = 0; i < 24; i++) fmt[i] = fgetc(file); // Läs formatet c.channels = getInt16(&fmt[10]); c.bitsPerSample = getInt16(&fmt[22]); c.sampleFreq = getInt32(&fmt[12]); unsigned char dataHeader[8]; for (int i = 0; i < 8; i++) dataHeader[i] = fgetc(file); c.size = getInt32(&dataHeader[4]); // Läs datan c.data = calloc(1, c.size); for (int i = 0; i < c.size; i++) c.data[i] = fgetc(file); // Skicka datan till OpenAL alGenBuffers(1, &c.bufferID); alBufferData(c.bufferID, c.bitsPerSample == 8 ? (c.channels == 1 ? AL_FORMAT_MONO8 : AL_FORMAT_STEREO8) : (c.channels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16), c.data, c.size, c.sampleFreq); fclose(file); return c; }
void SYS_InitSound() { #ifdef SYS_SOUNDDEBUG soundsLoaded = 0; soundsFree = SIZEOFWAVEPOOL; soundLog = fopen( soundLogFileName, "w" ); fprintf(soundLog, "SYS_InitSound() - OpenAL SKU - %d sounds in pool\n", soundsFree); if (alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT") == AL_TRUE) { const char* devicesStr = alcGetString(0, ALC_DEVICE_SPECIFIER); const char* defaultDeviceStr = alcGetString(0, ALC_DEFAULT_DEVICE_SPECIFIER); fprintf(soundLog, "OpenAL default device = %s\n", defaultDeviceStr); fprintf(soundLog, "All devices:\n"); const char* p = devicesStr; while( *p ) { fprintf(soundLog, "%s\n", p); p += (strlen(p) + 1); } } #endif // open device ALCdevice* device = alcOpenDevice(0); if ( !device ) { SYS_Error("Error opening OpenAL device\n"); return; } #ifdef SYS_SOUNDDEBUG const char* extStr = alcGetString(device, ALC_EXTENSIONS); fprintf(soundLog, "OpenAL extentions : %s\n", extStr); #endif alGetError(); // clear error code // create context ALCcontext* context = alcCreateContext(device,0); alcMakeContextCurrent(context); CheckForErrors(); // use linear distance model & disable doppler. alDistanceModel(AL_LINEAR_DISTANCE_CLAMPED); alDopplerFactor(0.0f); // init wave pools memset(wavePool, 0, sizeof(struct SYS_Wave) * SIZEOFWAVEPOOL); freeList = wavePool; usedList = 0; for ( int i = 0; i < SIZEOFWAVEPOOL; ++i ) { freeList[i].prev = (i == 0) ? 0 : &wavePool[i-1]; freeList[i].next = (i == SIZEOFWAVEPOOL-1) ? 0 : &wavePool[i+1]; } // init source pool memset(sourcePool, 0, sizeof(ALuint) * SIZEOFSOURCEPOOL); CheckForErrors(); int error = mpg123_init(); if (error != MPG123_OK) { printf("Error mpg123_init() : %s\n", mpg123_plain_strerror(error)); } }