void vsFailedAssert( const vsString &conditionStr, const vsString &msg, const char *file, int line ) { if ( !bAsserted ) { // failed assertion.. trace out some information and then crash. bAsserted = true; vsString trimmedFile(file); size_t pos = trimmedFile.rfind('/'); if ( pos ) { trimmedFile = trimmedFile.substr(pos+1); } vsLog("Failed assertion: %s", msg.c_str()); vsLog("Failed condition: (%s)", conditionStr.c_str()); vsLog("at %s:%d", trimmedFile.c_str(), line); vsLog_End(); { #if defined(_DEBUG) DEBUG_BREAK; #else vsString mbString = vsFormatString("Failed assertion: %s\nFailed condition: (%s)\nat %s:%d", msg.c_str(), conditionStr.c_str(), trimmedFile.c_str(), line); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Failed assertion", mbString.c_str(), NULL); #endif CRASH; } } else { vsLog("Error: Asserted while handling assertion!"); } }
void vsSoundSystem::Deinit() { StopMusic(); #if !TARGET_OS_IPHONE Mix_HaltChannel(-1); #endif vsLog(" ++ Sound channels in use: %d", m_channelsInUse); vsLog(" ++ Max sound channels in use: %d", m_maxChannelsInUse); }
vsSocket::vsSocket(int port): m_privateIP(0), m_privatePort(0), //m_publicIP(0), //m_publicPort(0), m_listener(NULL) { UNUSED(port); #ifndef _WIN32 vsLog("Opening socket"); m_socket = socket(AF_INET, SOCK_DGRAM, 0); if ( m_socket == -1 ) { perror("socket"); vsAssert( m_socket != -1, vsFormatString("Socket error: See console output for details" ) ); } sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = htonl(INADDR_ANY); memset(addr.sin_zero, '\0', sizeof addr.sin_zero); vsLog("Binding socket to port %d", port); int err = bind( m_socket, (sockaddr *)&addr, sizeof(addr) ); if ( err == -1 ) { perror("bind"); vsAssert( m_socket != -1, vsFormatString("Bind error: See console output for details" ) ); } socklen_t addrSize = sizeof(addr); err = getsockname(m_socket, (sockaddr*)&addr, &addrSize); if ( err == -1 ) { perror("getsockname"); vsAssert( m_socket != -1, vsFormatString("Getsockname error: See console output for details" ) ); } m_privateIP = ntohl(addr.sin_addr.s_addr); m_privatePort = ntohs(addr.sin_port); if ( m_privateIP == 0 ) // should be { vsLog("Bind succeeded: Listening on INADDR_ANY:%d", m_privatePort); } else { char * byte = (char *)&m_privateIP; vsLog("Bind succeeded: Listening on %d.%d.%d.%d:%d", byte[0], byte[1], byte[2], byte[3], m_privatePort); } #endif //_WIN32 }
void vsSocket::SendTo( vsNetClient *to, vsStore *packet ) { packet->Rewind(); char *buffer = packet->GetReadHead(); int len = packet->Length(); int flags = 0; sockaddr_in destAddr; destAddr.sin_family = AF_INET; destAddr.sin_port = htons(to->GetPort()); destAddr.sin_addr.s_addr = to->GetIP(); // don't convert this to network byte order; it's already stored that way! memset(destAddr.sin_zero, '\0', sizeof destAddr.sin_zero); int n = sendto(m_socket, buffer, len, flags, (sockaddr *)&destAddr, sizeof(destAddr)); if ( n != len ) { if ( n == -1 ) { perror("sendto"); } else { vsLog("Error: Expected to write %d bytes, actually wrote %d bytes!", len, n); } vsAssert( n == len, "Sendto error: Check console for details." ); } }
vsSoundSample::vsSoundSample(const vsString &filename_in) { #if !TARGET_OS_IPHONE const vsString &filename = vsFile::GetFullFilename(filename_in); m_chunk=Mix_LoadWAV(filename.c_str()); if(!m_chunk) { vsLog("Mix_LoadWAV(\"%s\"): %s", filename.c_str(), Mix_GetError()); // this might be a critical error... } #endif }
void vsLog(const char *format, ...) { va_list va; va_start(va, format); vsnprintf(sz, 1024*10, format, va); va_end(va); vsString str = sz; vsLog(str); }
void vsLog(const char *format, ...) { char sz[1024]; va_list marker; va_start(marker, format); vsprintf(sz, format, marker); va_end(marker); vsString str = sz; vsLog(str); }
void vsSoundSystem::PlayMusic( vsMusic * music ) { #if !TARGET_OS_IPHONE if ( music ) { if(Mix_PlayMusic(music->m_music, -1)==-1) { vsLog("Mix_PlayMusic: %s", Mix_GetError()); // well, there's no music, but most games don't break without music... } } else StopMusic(); #endif }
vsSoundSystem::vsSoundSystem() { s_instance = this; vsLog(" ++ Initialising mixer"); // if ( Mix_OpenAudio( MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024 ) ) #if !TARGET_OS_IPHONE if ( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 1024 ) ) vsLog(" !! Mix_OpenAudio: %s", Mix_GetError()); int numtimesopened, frequency, channels; Uint16 format; numtimesopened=Mix_QuerySpec(&frequency, &format, &channels); if(!numtimesopened) { vsLog(" !! Mix_QuerySpec: %s",Mix_GetError()); } else { const char *format_str="Unknown"; switch(format) { case AUDIO_U8: format_str="U8"; break; case AUDIO_S8: format_str="S8"; break; case AUDIO_U16LSB: format_str="U16LSB"; break; case AUDIO_S16LSB: format_str="S16LSB"; break; case AUDIO_U16MSB: format_str="U16MSB"; break; case AUDIO_S16MSB: format_str="S16MSB"; break; } vsLog(" ++ audio frequency=%dHz format=%s channels=%d", frequency, format_str, channels); } const char * soundDriver = SDL_GetAudioDeviceName(0, 0); if ( soundDriver ) vsLog(" ++ Sound playing using %s.", soundDriver); else vsLog(" ?? No sound driver reported by SDL_AudioDriverName."); m_channelCount = Mix_AllocateChannels(32); // get ourselves 32 channels for sound effects. That's pretty realistic for a SNES-era console. m_maxChannelsInUse = 0; m_channelsInUse = 0; Mix_ChannelFinished( &vsSoundSystem::ChannelFinished ); #endif }
void vsTextureInternal::ProcessSurface( SDL_Surface *source ) { // SDL_Surface *screen = SDL_GetVideoSurface(); SDL_Rect area; //SDL_SetAlpha(source, 0, SDL_ALPHA_OPAQUE); m_width = source->w; m_height = source->h; int w = power_of_two(source->w); int h = power_of_two(source->h); m_glTextureWidth = w; m_glTextureHeight = h; SDL_Surface *image = SDL_CreateRGBSurface( SDL_SWSURFACE, w, h, 32, #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 #else 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF #endif ); vsAssert(image, "Error??"); SDL_BlendMode bm; SDL_GetSurfaceBlendMode(source, &bm); if ( bm != SDL_BLENDMODE_NONE ) { SDL_SetSurfaceBlendMode(source, SDL_BLENDMODE_NONE); } /* Copy the surface into the GL texture image */ area.x = 0; area.y = 0; area.w = source->w; area.h = source->h; SDL_BlitSurface(source, &area, image, &area); /* Restore the alpha blending attributes */ if ( bm != SDL_BLENDMODE_NONE ) { SDL_SetSurfaceBlendMode(source, bm); } /* Create an OpenGL texture for the image */ GLuint t; glGenTextures(1, &t); m_texture = t; glBindTexture(GL_TEXTURE_2D, m_texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); // glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, image->pixels); glGenerateMipmap(GL_TEXTURE_2D); #if 0 /* if ( 0 ) { int wid = w; int hei = h; int mapMapId = 1; while ( wid > 32 || hei > 32 ) { wid = wid >> 1; hei = hei >> 1; glTexImage2D(GL_TEXTURE_2D, mapMapId++, GL_RGBA, wid, hei, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); } glGenerateMipmapEXT(GL_TEXTURE_2D); }*/ // now lets read it back. #endif // 0 bool doIt = false; if ( doIt ) { unsigned char *imageData = new unsigned char[w*h*4]; glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, imageData); for ( int i = 0; i < w*h; i++ ) { int ri = i*4; int gi = ri+1; int bi = ri+2; int ai = ri+3; unsigned char r = imageData[ri]; unsigned char g = imageData[gi]; unsigned char b = imageData[bi]; unsigned char a = imageData[ai]; unsigned char origr = ((unsigned char*)image->pixels)[ri]; unsigned char og = ((unsigned char*)image->pixels)[gi]; unsigned char ob = ((unsigned char*)image->pixels)[bi]; unsigned char oa = ((unsigned char*)image->pixels)[ai]; if ( r != origr || g != og || b != ob || a != oa ) { vsLog("Pixel %d,%d,%d,%d != %d,%d,%d,%d", r,g,b,a,origr,og,ob,oa); } } delete [] imageData; } //#endif //0 SDL_FreeSurface(image); /* No longer needed */ }
vsMatrix4x4 vsMatrix4x4::Inverse() const { /* NB. OpenGL Matrices are COLUMN major. */ /* Here's some shorthand converting standard (row,column) to index. */ if( m41 != 0.0f || m42 != 0.0f || m43 != 0.0f || m44 != 1.0f ) { return InverseGeneral(); } /* Inverse = adjoint / det. (See linear algebra texts.)*/ /* Allow out == in, but don't do an extra copy */ vsMatrix4x4 temp; temp.x.x= m22 * m33 - m23 * m32; temp.x.y= m23 * m31 - m21 * m33; temp.x.z= m21 * m32 - m22 * m31; /* Compute determinant as early as possible using these cofactors. */ register float det; det= m11 * temp.x.x + m12 * temp.x.y + m13 * temp.x.z; /* Run singularity test. */ if (det == 0.0) { vsLog("invert_matrix: Warning: Singular matrix."); return vsMatrix4x4::Identity; } float d12, d13, d23, d24, d34, d41; register float im11, im12, im13, im14; det= 1.0f / det; /* Compute rest of inverse. */ temp.x.x *= det; temp.x.y *= det; temp.x.z *= det; temp.x.w = 0.0f; im11= m11 * det; im12= m12 * det; im13= m13 * det; im14= m14 * det; temp.y.x = im13 * m32 - im12 * m33; temp.y.y = im11 * m33 - im13 * m31; temp.y.z = im12 * m31 - im11 * m32; temp.y.w = 0.0f; /* Pre-compute 2x2 dets for first two rows when computing */ /* cofactors of last two rows. */ d12 = im11*m22 - m21*im12; d13 = im11*m23 - m21*im13; d23 = im12*m23 - m22*im13; d24 = im12*m24 - m22*im14; d34 = im13*m24 - m23*im14; d41 = im14*m21 - m24*im11; temp.z.x = d23; temp.z.y = -d13; temp.z.z = d12; temp.z.w = 0.0f; temp.w.x = -(m32 * d34 - m33 * d24 + m34 * d23); temp.w.y = (m31 * d34 + m33 * d41 + m34 * d13); temp.w.z = -(m31 * d24 + m32 * d41 + m34 * d12); temp.w.w = 1.0f; #undef m11 #undef m12 #undef m13 #undef m14 #undef m21 #undef m22 #undef m23 #undef m24 #undef m31 #undef m32 #undef m33 #undef m34 #undef m41 #undef m42 #undef m43 #undef m44 return temp; }
vsDisplayList * vsDisplayList::Load_Obj( const vsString &filename ) { int vertCount = 0; int faceIndexCount = 0; vsDisplayList *loader = new vsDisplayList(1024 * 200); // 50k should be enough to load in just about anything. (famous last words) vsFile *file = new vsFile(filename + vsString(".obj")); vsRecord r; vsString faceStr("f"); vsString vertStr("v"); while( file->Record(&r) ) { vsString label = r.GetLabel().AsString(); if ( label == vertStr ) vertCount++; if ( label == faceStr ) faceIndexCount+=3; } file->Rewind(); vsVector3D *vertexPos = new vsVector3D[vertCount]; int *triangleIndex = new int[faceIndexCount]; int v = 0; int f = 0; while( file->Record(&r) ) { vsString label = r.GetLabel().AsString(); if ( label == vertStr ) vertexPos[v++].Set(r.GetToken(0).AsFloat(), -r.GetToken(1).AsFloat(), 0.f); if ( label == faceStr ) { triangleIndex[f++] = r.GetToken(0).AsInteger()-1; triangleIndex[f++] = r.GetToken(1).AsInteger()-1; triangleIndex[f++] = r.GetToken(2).AsInteger()-1; } } loader->VertexArray( vertexPos, vertCount ); loader->TriangleListArray( triangleIndex, faceIndexCount ); delete file; vsAssert(loader->GetSize() > 0, "Didn't get any operations in a loaded display list!" ) vsLog("Display list is %d bytes", loader->GetSize()); vsDisplayList *result = new vsDisplayList( loader->GetSize() ); result->Append(*loader); delete loader; vsDeleteArray(vertexPos); vsDeleteArray(triangleIndex); return result; }