void NoiseTable::add( int granu, float min, float max ) { assertmsg( width % granu == 0, "invalid granu"); assertmsg( height % granu == 0, "invalid granu"); int tmpw = width / granu + 1, tmph = height / granu + 1; size_t sz = sizeof(float) * tmpw * tmph; float *tmptbl = (float*) MALLOC( sz ); for(int y=0;y<tmph;y++){ // fprintf(stderr, "tmp[%d]: ", y ); for(int x=0;x<tmpw;x++){ tmptbl[x+y*tmpw] = range(min,max); // fprintf(stderr, "%.4f", tmptbl[x+y*tmpw]); } // fprintf(stderr,"\n"); } for(int y=0;y<height;y++){ for(int x=0;x<width;x++){ int tmpx = x/granu, tmpy = y/granu; float ltv = tmptbl[ tmpx + tmpy * tmpw ]; float lbv = tmptbl[ tmpx + (tmpy+1) * tmpw ]; float rtv = tmptbl[ tmpx+1 + tmpy * tmpw ]; float rbv = tmptbl[ tmpx+1 + (tmpy+1) * tmpw ]; float yratio = (float)(y%granu) / (float)(granu); float xratio = (float)(x%granu) / (float)(granu); float left_val = interpolate( ltv, lbv, yratio ); float right_val = interpolate( rtv, rbv, yratio ); float out = interpolate( left_val, right_val, xratio ); set( x,y, get(x,y) + out ); } } FREE(tmptbl); }
void VertexBuffer::copyFromBuffer( float *v, int vert_cnt ) { assertmsg( unit_num_float > 0, "call setFormat() before this." ); assertmsg( vert_cnt <= array_len, "size too big"); array_len = vert_cnt; total_num_float = vert_cnt * unit_num_float; memcpy( buf, v, vert_cnt * unit_num_float * sizeof(float) ); }
Vec2 VertexBuffer::getUV( int index ) { assertmsg(fmt, "vertex format is not set" ); assert( index < array_len ); int ofs = fmt->texture_offset; assertmsg( ofs >= 0, "texuv have not declared in vertex format" ); int index_in_array = index * unit_num_float + ofs; return Vec2( buf[index_in_array], buf[index_in_array+1] ); }
Vec3 VertexBuffer::getNormal( int index ) { assertmsg(fmt, "vertex format is not set" ); assert( index < array_len ); int ofs = fmt->normal_offset; assertmsg( ofs >= 0, "normal have not declared in vertex format" ); int index_in_array = index * unit_num_float + ofs; return Vec3( buf[index_in_array], buf[index_in_array+1], buf[index_in_array+2] ); }
Color VertexBuffer::getColor( int index ) { assertmsg(fmt, "vertex format is not set" ); assert( index < array_len ); int ofs = fmt->color_offset; assertmsg( ofs >= 0, "color have not declared in vertex format" ); int index_in_array = index * unit_num_float + ofs; return Color( buf[index_in_array], buf[index_in_array+1], buf[index_in_array+2], buf[index_in_array+3] ); }
int memCompressSnappy( char *out, int outlen, char *in, int inlen ) { size_t maxsz = snappy_max_compressed_length(inlen); assertmsg( outlen >= maxsz, "snappy requires buffer size:%d given:%d", maxsz, outlen ); size_t osz = outlen; snappy_status ret = snappy_compress( in, inlen, out, &osz); if(ret == SNAPPY_OK ) return (int)osz; else assertmsg(false,"snappy_compress failed. outlen:%d inlen:%d ret:%d", outlen, inlen,ret ); return 0; }
void VertexBuffer::setUV( int index, Vec2 uv ) { assertmsg(fmt, "vertex format is not set"); assert( index < array_len ); int ofs = fmt->texture_offset; assertmsg( ofs >= 0, "texcoord have not declared in vertex format"); int index_in_array = index * unit_num_float + ofs; buf[index_in_array] = uv.x; buf[index_in_array+1] = uv.y; }
void VertexBuffer::setNormal( int index, Vec3 v ) { assertmsg(fmt, "vertex format is not set"); assert( index < array_len ); int ofs = fmt->normal_offset; assertmsg( ofs >= 0, "normal have not declared in vertex format" ); int index_in_array = index * unit_num_float + ofs; buf[index_in_array] = v.x; buf[index_in_array+1] = v.y; buf[index_in_array+2] = v.z; }
void VertexBuffer::setCoord( int index, Vec3 v ) { assertmsg(fmt, "vertex format is not set" ); assertmsg( index < array_len, "invalid index:%d array_len:%d", index, array_len ); int ofs = fmt->coord_offset; assertmsg( ofs >= 0, "coord have not declared in vertex format" ); int index_in_array = index * unit_num_float + ofs; buf[index_in_array] = v.x; buf[index_in_array+1] = v.y; buf[index_in_array+2] = v.z; }
void VertexBuffer::setColor( int index, Color c ) { assertmsg(fmt, "vertex format is not set"); assert( index < array_len ); int ofs = fmt->color_offset; assertmsg( ofs >= 0, "color have not declared in vertex format"); int index_in_array = index * unit_num_float + ofs; buf[index_in_array] = c.r; buf[index_in_array+1] = c.g; buf[index_in_array+2] = c.b; buf[index_in_array+3] = c.a; }
bool Image::writePNGMem( unsigned char **out, size_t *outsize ) { assertmsg( buffer!=0 , "image not initialized?" ); assertmsg( width <= 2048 && height <= 2048, "image too big" ); unsigned error; error = lodepng_encode32( out, outsize, buffer, width, height ); if(error) { fprintf(stderr, "lodepng_encode32_file failed%d", error ); return false; } return true; }
bool Image::writePNG(const char *path) { const char *cpath = platformCStringPath(path); assertmsg( buffer!=0 , "image not initialized?" ); assertmsg( width <= 2048 && height <= 2048, "image too big" ); /*Same as lodepng_encode_file, but always encodes from 32-bit RGBA raw image.*/ unsigned error; error = lodepng_encode32_file( cpath, buffer, width, height ); if(error) { fprintf(stderr, "lodepng_encode32_file failed%d", error ); return false; } return true; }
DrawBatch *DrawBatchList::startNextBatch( Viewport *vp, VFTYPE vft, GLuint tex, GLuint primtype, FragmentShader *fs, BLENDTYPE bt, int linew ) { assertmsg( used<MAXBATCH, "max draw batch (vftype). need tune" ); DrawBatch *b = new DrawBatch( vp, vft, tex, primtype, fs, bt, linew ); batches[used] = b; used++; return b; }
DrawBatch *DrawBatchList::startNextMeshBatch( Viewport *vp, FragmentShader *fs, BLENDTYPE bt, GLuint tex, Vec2 tr, Vec2 scl, float radrot, Mesh *mesh, bool copy_mesh ) { assertmsg( used<MAXBATCH, "max draw batch (withshader). need tune" ); DrawBatch *b = new DrawBatch( vp, fs, bt, tex, mesh->prim_type, tr, scl, radrot, mesh, copy_mesh ); batches[used] = b; used++; return b; }
void Effect::EndPass() { traceInFast(Effect::EndPass); assertmsg(curPass, TEXT("Called EndPass while not in a pass.")); for(int i=0; i<curPass->PixelParams.Num(); i++) { PassParam ¶m = curPass->PixelParams[i]; param.param->curPShaderParam = NULL; param.param->bChanged = FALSE; param.param->bValid = FALSE; if(param.param->type == Parameter_Texture) curPass->pixelShader->SetTexture(param.handle, NULL); } for(int i=0; i<curPass->VertParams.Num(); i++) { PassParam ¶m = curPass->VertParams[i]; param.param->curVShaderParam = NULL; param.param->bChanged = FALSE; param.param->bValid = FALSE; if(param.param->type == Parameter_Texture) curPass->vertShader->SetTexture(param.handle, NULL); } curPass = NULL; firstChanged = NULL; system->LoadPixelShader(NULL); system->LoadVertexShader(NULL); traceOutFast; }
void Effect::BeginPassByHandle(HANDLE hPass) { traceInFast(Effect::BeginPassByHandle); assertmsg(!curPass, TEXT("Called BeginPass while already in a pass.")); assert(curTech); if(!curTech) return; curPass = ConvertPass(hPass); system->LoadPixelShader(curPass->pixelShader); system->LoadVertexShader(curPass->vertShader); for(int j=0; j<curPass->PixelParams.Num(); j++) { PassParam ¶m = curPass->PixelParams[j]; param.param->curPShaderParam = param.handle; param.param->bValid = TRUE; } for(int j=0; j<curPass->VertParams.Num(); j++) { PassParam ¶m = curPass->VertParams[j]; param.param->curVShaderParam = param.handle; param.param->bValid = TRUE; } UploadAllShaderParams(); traceOutFast; }
void SoundSystem::setVolume(float v ) { #if defined(USE_UNTZ) UNTZ::System::get()->setVolume(v); #else assertmsg(false, "not implemented"); #endif }
void VertexBuffer::reserve(int cnt) { assertmsg(fmt, "vertex format is not set" ); array_len = cnt; unit_num_float = fmt->getNumFloat(); total_num_float = array_len * unit_num_float; buf = (float*)MALLOC( total_num_float * sizeof(float)); assert(buf); }
Viewport::Viewport(Client *cl) : screen_width(0), screen_height(0), dimension(DIMENSION_2D), scl(0,0,0), near_clip(0.01f), far_clip(100), tracker(0), remote_client(cl) { if(cl) { if(cl->target_viewport) { assertmsg(false,"client already have target_viewport set"); } cl->target_viewport = this; } id = id_gen++; }
void SoundSystem::append( Sound *s ) { for(int i=0;i<elementof(sounds);i++) { if( sounds[i] == NULL ) { sounds[i] = s; return; } } assertmsg(false, "sound full"); }
void convertFieldData( char *indir, char *outdir, int pjid ) { char outpath[400]; snprintf(outpath, sizeof(outpath), "field_data_%d_%d_%d_%d", pjid, FIELD_W, FIELD_H, (int)sizeof(Cell) ); assertmsg( sizeof(Cell) == 48, "invalid Cell size compiled:%d. Expect 48. incorrect version?", sizeof(Cell) ); char outfullpath[400]; makeFullPath( outdir, outfullpath, sizeof(outfullpath), outpath ); if( fileExist(outfullpath) ) { print("FATAL: File exists:'%s'", outfullpath); return; } size_t read_total = 0; int chw = FIELD_W / CHUNKSZ; int chh = FIELD_H / CHUNKSZ; for(int chy=0;chy<chh;chy++) { for( int chx=0;chx<chw;chx++) { char path[400]; snprintf( path, sizeof(path), "field_data_%d_%d_%d_%d_%d", pjid, chx*CHUNKSZ, chy*CHUNKSZ, CHUNKSZ, CHUNKSZ ); char fullpath[400]; makeFullPath( indir, fullpath, sizeof(fullpath), path ); char buf[32*1024]; size_t sz = sizeof(buf); bool res = readFile( fullpath, buf, &sz ); assertmsg( res, "can't read file:'%s'", fullpath); if(chx%8==0)prt("."); read_total += sz; if( chx==chw-1 ) print("chunk y:%d total:%d",chy, read_total ); assertmsg( sz < sizeof(buf), "saved file is too large:'%s'", fullpath ); char decompressed[sizeof(Cell)*CHUNKSZ*CHUNKSZ]; int decomplen = memDecompressSnappy( decompressed, sizeof(decompressed), buf, sz ); assertmsg(decomplen == sizeof(decompressed), "invalid decompressed size:%d file:'%s'", decomplen, fullpath ); int chunk_index = chx + chy*chw; size_t offset = chunk_index * sizeof(Cell)*CHUNKSZ*CHUNKSZ; // print("write. ofs:%d chx:%d chy:%d", offset, chx, chy ); res = writeFileOffset( outfullpath, decompressed, decomplen, offset, false ); assertmsg(res, "writeFileOffset failed for '%s'", outfullpath ); } } }
GROUNDTYPE charToGT( char ch ) { switch(ch) { case '.': return GT_SPACE; case 'x': return GT_PANEL; case 'Z': return GT_HATCH; default: assertmsg(false, "invalid ship data: ch:%c", ch ); } return GT_SPACE; }
void NoiseTable::noise( int maxgranu, float min, float max ) { assertmsg( isPowerOf2( (unsigned int) maxgranu), "need power of 2" ); float m = max; int granu = maxgranu; for(;;){ m /= 2.0; add( granu, min, m ); granu /= 2; if( granu == 0 ) break; } }
static void tm_thread_destroy(unsigned long data) { struct thread *thr = (struct thread *)data; assert(thr != current_thread); /* if the thread still hasn't been rescheduled, don't destroy it yet */ assert(thr->state == THREADSTATE_DEAD); assertmsg(thr->flags & THREAD_DEAD, "tried to destroy a thread before it has scheduled away"); tm_thread_release_stacks(thr); tm_process_put(thr->process); /* thread releases it's process pointer */ tm_thread_put(thr); }
// Returns 4 bits: RT-LT-RB-LB int Field::getEnterableBits( Vec2 at, float sz, bool flying ) { if(flying) return 0; assertmsg( sz < PPC, "too big" ); int out = 0; Cell *rtc = get( at + Vec2( sz, sz ) ); Cell *ltc = get( at + Vec2( -sz, sz ) ); Cell *rbc = get( at + Vec2( sz, -sz ) ); Cell *lbc = get( at + Vec2( -sz, -sz ) ); if( (!rtc) || (!rtc->isEnterable(flying)) ) out += WALKABLE_BIT_HIT_RT; if( (!ltc) || (!ltc->isEnterable(flying)) ) out += WALKABLE_BIT_HIT_LT; if( (!rbc) || (!rbc->isEnterable(flying)) ) out += WALKABLE_BIT_HIT_RB; if( (!lbc) || (!lbc->isEnterable(flying)) ) out += WALKABLE_BIT_HIT_LB; return out; }
inline void Effect::UpdateParams() { assertmsg(curPass, TEXT("Called UpdateParams while not in a pass.")); EffectParam *param = firstChanged; if(!param) return; while(param) { if(param->curVShaderParam) UploadParam(curPass->vertShader, param, param->curVShaderParam); if(param->curPShaderParam) UploadParam(curPass->pixelShader, param, param->curPShaderParam); param->bChanged = FALSE; param = param->nextChanged; } firstChanged = NULL; }
VertexFormat *DrawBatch::getVertexFormat(VFTYPE t) { switch(t) { case VFTYPE_COORD_COLOR: if(!g_vf_coord_color) { g_vf_coord_color = new VertexFormat(); g_vf_coord_color->declareCoordVec3(); g_vf_coord_color->declareColor(); } return g_vf_coord_color; case VFTYPE_COORD_COLOR_UV: if(!g_vf_coord_color_tex) { g_vf_coord_color_tex = new VertexFormat(); g_vf_coord_color_tex->declareCoordVec3(); g_vf_coord_color_tex->declareColor(); g_vf_coord_color_tex->declareUV(); } return g_vf_coord_color_tex; default: assertmsg(false, "invalid vftype"); } return NULL; }
void Layer_D3D::selectCenterInside( Vec2 minloc, Vec2 maxloc, Prop*out[], int *outlen ) { assertmsg( viewport->dimension == DIMENSION_2D, "selectCenterInside isn't implemented for 3d viewport" ); Prop *cur = prop_top; int out_max = *outlen; int cnt=0; while(cur){ Prop2D_D3D *cur2d = (Prop2D_D3D*) cur; if( cur2d->dimension == DIMENSION_2D ){ if( !cur->to_clean && cur2d->isCenterInside(minloc, maxloc) ){ if( cnt < out_max){ out[cnt] = cur; cnt++; if(cnt==out_max)break; } } } cur = cur->next; } *outlen = cnt; }
void IndexBuffer::copyFromBuffer( IndexBufferType *inbuf, int ind_cnt ) { assertmsg( ind_cnt <= array_len, "size too big"); render_len = ind_cnt; memcpy( buf, inbuf, ind_cnt * sizeof(IndexBufferType) ); }
void Keyboard::validateKeyCode(int keycode) { assertmsg( keycode < MOYAI_KEYCODE_MAX, "keycode out of range:%d", keycode ); }