Skip to content

Commit

Permalink
GL tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
kfprimm committed May 9, 2017
1 parent e55ac40 commit 4c49d08
Show file tree
Hide file tree
Showing 22 changed files with 139 additions and 37 deletions.
2 changes: 1 addition & 1 deletion compiler/libs.cpp
Expand Up @@ -136,7 +136,7 @@ static const char *linkRuntime(){
}
#else
json index;
ifstream i(home+"/lib/default." BB_PLATFORM ".config.json");
ifstream i(home+"/lib/opengl." BB_PLATFORM ".config.json");
i >> index;

for( auto& module:index["modules"] ) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/stmtnode.h
Expand Up @@ -181,7 +181,7 @@ struct ExitNode : public StmtNode{
void translate( Codegen *g );

json toJSON( Environ *e ){
json tree;tree["@class"]="IfNode";
json tree;tree["@class"]="ExitNode";
tree["pos"]=pos;
tree["sem_brk"]=sem_brk;
return tree;
Expand Down
2 changes: 2 additions & 0 deletions config/config.h
Expand Up @@ -27,9 +27,11 @@
#define VERSION (BASE_VER|PRO_F|DEMO_F|EDU_F)

#if INTPTR_MAX == INT64_MAX
#define BB64
typedef long bb_int_t;
typedef double bb_float_t;
#else
#define BB32
typedef int bb_int_t;
typedef float bb_float_t;
#endif
Expand Down
1 change: 1 addition & 0 deletions lib/blitz3d/translator/node.rb
Expand Up @@ -40,6 +40,7 @@ def indent
require_relative './node/stmt'
require_relative './node/ass'
require_relative './node/delete'
require_relative './node/exit'
require_relative './node/include'
require_relative './node/for'
require_relative './node/for_each'
Expand Down
8 changes: 8 additions & 0 deletions lib/blitz3d/translator/node/cast.rb
Expand Up @@ -22,6 +22,14 @@ def to_c
else
raise "Need to handle StringType->#{expr.sem_type.class.name} casts"
end
elsif type.is_a?(IntType)
if expr.sem_type.is_a?(StringType)
"_bbStrToInt(#{expr.to_c})"
elsif expr.sem_type.is_a?(FloatType)
"(bb_float_t)(#{expr.to_c})"
else
raise "Need to handle IntType->#{expr.sem_type.class.name} casts"
end
else
"(#{type.to_c})(#{expr.to_c})"
end
Expand Down
16 changes: 16 additions & 0 deletions lib/blitz3d/translator/node/exit.rb
@@ -0,0 +1,16 @@
module Blitz3D
module AST
class ExitNode < StmtNode
attr_accessor :sem_brk

def initialize(json)
super
@sem_brk = json['sem_brk']
end

def to_c
'break'
end
end
end
end
2 changes: 1 addition & 1 deletion lib/blitz3d/translator/node/repeat.rb
Expand Up @@ -12,7 +12,7 @@ def initialize(json)
end

def to_c(&cleanup)
"do{\n #{stmts.to_c(&cleanup).indent}\n}while( #{expr.try(:to_c) || 1} )"
"do{\n #{stmts.to_c(&cleanup).indent}\n}while( !#{expr.try(:to_c) || 1} )"
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/blitz3d/translator/node/stmt_seq.rb
Expand Up @@ -14,8 +14,8 @@ def to_c(&cleanup)
f.write(@stmts.map do |stmt|
code = stmt.to_c(&cleanup)
code = (code.last != '}' && code.last != ';') ? "#{code};" : code
# "\n_bbDebugStmt(#{stmt.pos},#{file.inspect});\n#{code}"
"#{code}\n"
"\n_bbDebugStmt(#{stmt.pos},#{file.inspect});\n#{code}"
# "#{code}\n"
end.join(""))

f.string
Expand Down
6 changes: 3 additions & 3 deletions lib/blitz3d/translator/node/var_decl.rb
Expand Up @@ -9,8 +9,8 @@ def initialize(json)
@tag = json['tag']
@kind = json['kind']
@constant = json['constant']
@expr = Node.load(json['expr'])
@sem_var = Node.load(json['sem_var'])
@expr = Node.load(json['expr']) if json['expr']
@sem_var = Node.load(json['sem_var']) if json['sem_var']
end

def to_h
Expand All @@ -19,7 +19,7 @@ def to_h
end

def to_c
expr = self.expr || IntConstNode.new(value: 0)
expr = self.expr || IntConstNode.new('value' => 0)
"#{ident}=#{expr.to_c}"
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/blitz3d/translator/type/const.rb
Expand Up @@ -16,9 +16,9 @@ def to_c

def default_value
if value_type.is_a?(IntType)
int_value
int_value || 0
elsif value_type.is_a?(FloatType)
float_value
float_value || 0.0
elsif value_type.is_a?(StringType)
string_value.inspect
end
Expand Down
22 changes: 17 additions & 5 deletions src/runtime/bb/blitz/basic.cpp
Expand Up @@ -154,21 +154,33 @@ int BBCALL _bbStrCompare( BBStr *lhs,BBStr *rhs ){
delete lhs;delete rhs;return n;
}

int BBCALL _bbStrToInt( BBStr *s ){
bb_int_t BBCALL _bbStrToInt( BBStr *s ){
#ifdef BB32
int n=atoi( *s );
#else
long n=atol( *s );
#endif
delete s;return n;
}

BBStr * BBCALL _bbStrFromInt( int n ){
BBStr * BBCALL _bbStrFromInt( bb_int_t n ){
#ifdef BB32
return d_new BBStr( itoa( n ) );
#else
return d_new BBStr( ltoa( n ) );
#endif
}

float BBCALL _bbStrToFloat( BBStr *s ){
bb_float_t BBCALL _bbStrToFloat( BBStr *s ){
#ifdef BB32
float n=(float)atof( *s );
#else
double n=atof( *s );
#endif
delete s;return n;
}

BBStr * BBCALL _bbStrFromFloat( float n ){
BBStr * BBCALL _bbStrFromFloat( bb_float_t n ){
return d_new BBStr( ftoa( n ) );
}

Expand Down Expand Up @@ -250,7 +262,7 @@ static void insertObj( BBObj *obj,BBObj *next ){

BBObj * BBCALL _bbObjNew( BBObjType *type ){
if( type->free.next==&type->free ){
int obj_size=sizeof(BBObj)+type->fieldCnt*4;
int obj_size=sizeof(BBObj)+type->fieldCnt*sizeof(BBField);
BBObj *o=(BBObj*)bbMalloc( obj_size*OBJ_NEW_INC );
for( int k=0;k<OBJ_NEW_INC;++k ){
insertObj( o,&type->free );
Expand Down
12 changes: 6 additions & 6 deletions src/runtime/bb/blitz/commands.h
Expand Up @@ -85,8 +85,8 @@ struct BBVecType{
};

union BBField{
int INT;
float FLT;
bb_int_t INT;
bb_float_t FLT;
BBStr *STR;
char *CSTR;
BBObj *OBJ;
Expand Down Expand Up @@ -144,10 +144,10 @@ void BBCALL _bbStrStore( BBStr **var,BBStr *str );
int BBCALL _bbStrCompare( BBStr *lhs,BBStr *rhs );

BBStr * BBCALL _bbStrConcat( BBStr *s1,BBStr *s2 );
int BBCALL _bbStrToInt( BBStr *s );
BBStr * BBCALL _bbStrFromInt( int n );
float BBCALL _bbStrToFloat( BBStr *s );
BBStr * BBCALL _bbStrFromFloat( float n );
bb_int_t BBCALL _bbStrToInt( BBStr *s );
BBStr * BBCALL _bbStrFromInt( bb_int_t n );
bb_float_t BBCALL _bbStrToFloat( BBStr *s );
BBStr * BBCALL _bbStrFromFloat( bb_float_t n );
BBStr * BBCALL _bbStrConst( const char *s );

void BBCALL _bbDimArray( BBArray *array );
Expand Down
9 changes: 8 additions & 1 deletion src/runtime/bb/blitz2d.gl/blitz2d.gl.h
Expand Up @@ -129,6 +129,7 @@ class GLB2DCanvas : public BBCanvas{
void copyPixelFast( int x,int y,BBCanvas *src,int src_x,int src_y ){}
unsigned getPixel( int x,int y )const{ return 0; }
unsigned getPixelFast( int x,int y )const{
y=height-y;
return pixels[(y*width+x)*4];
}
void unlock()const{
Expand Down Expand Up @@ -206,7 +207,13 @@ class GLB2DTextureCanvas : public GLB2DCanvas{
unsigned int getTextureId(){ return texture; }

void setPixmap( BBPixmap *pm ){
if( flags&CANVAS_TEX_MASK ) pm->mask( 0,0,0 );
if( flags&CANVAS_TEX_ALPHA ){
if( flags&CANVAS_TEX_MASK ){
pm->mask( 0,0,0 );
}else{
pm->buildAlpha( false );
}
}

width=pm->width;
height=pm->height;
Expand Down
Empty file.
8 changes: 7 additions & 1 deletion src/runtime/bb/blitz3d.gl/blitz3d.gl.cpp
Expand Up @@ -312,6 +312,12 @@ class GLScene : public BBScene{
glLightModelfv( GL_LIGHT_MODEL_AMBIENT,ambient );
}

if( rs.fx&FX_VERTEXCOLOR ){
glEnable( GL_COLOR_MATERIAL );
}else{
glDisable( GL_COLOR_MATERIAL );
}

for( int i=0;i<MAX_TEXTURES;i++ ){
const RenderState::TexState &ts=rs.tex_states[i];
glActiveTexture( GL_TEXTURE0+i );
Expand Down Expand Up @@ -345,7 +351,7 @@ class GLScene : public BBScene{
bool mipmap=flags&BBCanvas::CANVAS_TEX_MIPMAP;

glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,no_filter?GL_NEAREST:GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,mipmap?GL_LINEAR_MIPMAP_LINEAR:(no_filter?GL_NEAREST:GL_LINEAR) );
glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,mipmap?GL_LINEAR_MIPMAP_LINEAR:(no_filter?GL_NEAREST:GL_LINEAR) );

if( flags&BBCanvas::CANVAS_TEX_CLAMPU ){
glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE );
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/bb/blitz3d/loader_assimp.cpp
Expand Up @@ -28,7 +28,7 @@ MeshModel *Loader_Assimp::parseNode( const struct aiNode* nd ){

aiColor4D diffuse;
if( aiGetMaterialColor( mtl,AI_MATKEY_COLOR_DIFFUSE,&diffuse )==AI_SUCCESS ){
b.setColor( Vector(&diffuse.r) );
b.setColor( Vector(diffuse.r,diffuse.g,diffuse.b) );
if( diffuse.a ) b.setAlpha( diffuse.a );
}

Expand Down Expand Up @@ -72,7 +72,7 @@ MeshModel *Loader_Assimp::parseNode( const struct aiNode* nd ){
nd->mTransformation.Decompose( scl,rot,pos );

m->setLocalPosition( Vector( pos[0],pos[1],pos[2] ) );
m->setLocalScale( Vector( scl[0],scl[1],scl[2] ) );
m->setLocalScale( Vector( scl[0],scl[1],scl[2] )*Vector(1,1,-1) );
m->setLocalRotation( Quat( rot.w,Vector( rot.x,rot.y,rot.z ) ) );

MeshLoader::endMesh( m );
Expand Down
25 changes: 20 additions & 5 deletions src/runtime/bb/pixmap/pixmap.cpp
@@ -1,6 +1,7 @@

#include "../../stdutil/stdutil.h"
#include <bb/blitz/module.h>
#include <bb/blitz/blitz.h>

#include <FreeImage.h>
#include <cstring>
#include "pixmap.h"
Expand Down Expand Up @@ -42,12 +43,18 @@ BBPixmap *bbLoadPixmap( const std::string &file ){

bool trans=FreeImage_GetBPP( t_dib )==32 || FreeImage_IsTransparent( t_dib );

FIBITMAP *dib=FreeImage_ConvertTo32Bits( t_dib );
BBPixmap *pm=d_new BBPixmap();

if( dib ) FreeImage_Unload( t_dib );
else dib=t_dib;
switch( int bpp=FreeImage_GetBPP( t_dib ) ){
case 32:pm->format=PF_RGBA;break;
case 24:pm->format=PF_RGB;break;
default:RTEX( ("Unhandled image format: "+string(itoa(bpp))+" bpps").c_str() );
}

BBPixmap *pm=d_new BBPixmap();
FIBITMAP *dib=FreeImage_ConvertTo32Bits( t_dib );

if( dib ) FreeImage_Unload( t_dib );
else dib=t_dib;

pm->width=FreeImage_GetWidth( dib );
pm->height=FreeImage_GetHeight( dib );
Expand Down Expand Up @@ -78,6 +85,14 @@ void BBPixmap::mask( int r,int g,int b ){
}
}

void BBPixmap::buildAlpha( bool whiten ){
// NASTY: gotta be a better way to do this...
for( int i=0;i<width*height;i++ ){
unsigned char *p=&bits[bpp*i];
p[3]=(p[0]+p[1]+p[2])/3;
}
}

BBMODULE_CREATE( pixmap ){
return true;
}
Expand Down
8 changes: 8 additions & 0 deletions src/runtime/bb/pixmap/pixmap.h
Expand Up @@ -3,14 +3,22 @@

#include <string>

enum{
PF_UNKNOWN=0,
PF_RGB,
PF_RGBA,
};

struct BBPixmap{
int format;
int width,height,depth,pitch,bpp;
unsigned char *bits;

BBPixmap();
~BBPixmap();

void mask( int r,int g,int b );
void buildAlpha( bool whiten );
};

BBPixmap *bbLoadPixmap( const std::string &file );
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/bb/runtime.glfw3/glfw3.premake5.lua
Expand Up @@ -16,7 +16,7 @@ project "glfw"
}

filter "platforms:macos"
defines "_GLFW_COCOA"
defines { "_GLFW_COCOA", "_GLFW_USE_RETINA" }

files {
"glfw/src/cocoa_platform.h",
Expand Down
28 changes: 22 additions & 6 deletions src/runtime/bb/stub/stub.macos.cpp
Expand Up @@ -14,9 +14,13 @@ void sue( const char *t ){

class StdioDebugger : public Debugger{
private:
string file;
int row,col;
bool trace;
string file;
int row,col;
public:
StdioDebugger( bool t ):trace(t){
}

void debugRun(){
}
void debugStop(){
Expand All @@ -25,7 +29,7 @@ class StdioDebugger : public Debugger{
file=string(f);
row=(srcpos>>16)&0xffff;
col=srcpos&0xffff;
// cout<<file<<":"<<"["<<row+1<<":"<<col<<"]"<<endl;
if( trace ) cout<<file<<":"<<"["<<row+1<<":"<<col<<"]"<<endl;
}
void debugEnter( void *frame,void *env,const char *func ){
}
Expand All @@ -49,11 +53,17 @@ int main( int argc,char *argv[] ){
bb_env.debug=false;
#endif

bool trace=false;
string cmd_line;
for( int i=1;i<argc;i++ ) cmd_line+=argv[i];
for( int i=1;i<argc;i++ ){
if( strncmp( "--trace",argv[i],strlen("--trace")+1 )==0 ){
trace=true;
}
cmd_line+=argv[i];
}
bbStartup( argv[0],cmd_line.c_str() );

StdioDebugger debugger;
StdioDebugger debugger( trace );
bbAttachDebugger( &debugger );

if( !(bbRuntime=bbCreateRuntime()) ){
Expand All @@ -66,7 +76,13 @@ int main( int argc,char *argv[] ){
if( !bbruntime_create() ) return 1;
bbMain();
}catch( bbEx &ex ){
if( ex.err ) cerr<<ex.err<<endl;
if( ex.err ){
if( bb_env.debug ){
debugger.debugLog( ex.err );
}else{
cerr<<ex.err<<endl;
}
}
retcode=1;
}
bbruntime_destroy();
Expand Down

0 comments on commit 4c49d08

Please sign in to comment.