Coding challenges?

For other topics related to the FreeBASIC project or its community.
BasicCoder2
Posts: 3310
Joined: Jan 01, 2009 7:03

Coding challenges?

Postby BasicCoder2 » Nov 03, 2017 22:20

fxm's FreeBASIC syntax challenges made me think maybe we could also have FreeBASIC coding challenges?
Perhaps duplicate the example challenges in the link below or find new short challenges and show how you can do them using FreeBASIC?
Some of the challenges in the utube series have already been done but a spread throughout the forum posts over many years.
They could be used to demo the OOP capabilities of FreeBASIC as well?

https://www.youtube.com/watch?v=LG8ZK-rRkXo
This challenge #2 was done in the processing environment so it might be hard to duplicate in FreeBASIC. It does show how easy some things like 3D can be done in another language. I would suggest this one is a good challenge for our 3D experts?

Code: Select all

void show() {
   pushMatrix();
   translate(pos.x, pos.y, pos.z) // to see that box
   box(r); // draw box
}

float a = 0;

void setup(){
  size(400,400,P3D);

}

void draw() {
   background(51);
   stroke(255);
   noFill();

   translate(width/2, height/2);
   rotateX(a)
   b.show();
   a += 0.01;

}
paul doe
Posts: 804
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Coding challenges?

Postby paul doe » Nov 04, 2017 1:34

Hi, BasicCoder2
BasicCoder2 wrote:fxm's FreeBASIC syntax challenges made me think maybe we could also have FreeBASIC coding challenges?
Perhaps duplicate the example challenges in the link below or find new short challenges and show how you can do them using FreeBASIC?

Yo, and the metaball demo you were coding? Was it not one of these 'coding challenges'? =D
BasicCoder2 wrote:Some of the challenges in the utube series have already been done but a spread throughout the forum posts over many years.
They could be used to demo the OOP capabilities of FreeBASIC as well?

Yeah, the inevitable consecuence of not having some specific place to put those XD. And speaking of OOP, what would be the purpose of this? The 'metaball' demo I coded is about as basic OOP as it gets (no pun intended, seriously) but you already stated that you couldn't understand a iota of it =D
BasicCoder2 wrote:This challenge #2 was done in the processing environment so it might be hard to duplicate in FreeBASIC. It does show how easy some things like 3D can be done in another language. I would suggest this one is a good challenge for our 3D experts?

Not particularly. The 3D Menger Sponge fractal can be easily coded in FB, you even have the means to do so. Remember the paperplane demo? You can already use it to code something like that (in wireframe, of course; I didn't implement other rendering modes)
Now, I don't consider myself a '3D expert', and I haven't used processing code in my life, but I'm reasonably sure that the code you posted shows a white cube rotating about it's X axis in the middle of the screen LOL

EDIT: Or perhaps it was a sample of how easy it is to do 3D coding in the processing environment? I'm not too bright today, sorry mate =D
BasicCoder2
Posts: 3310
Joined: Jan 01, 2009 7:03

Re: Coding challenges?

Postby BasicCoder2 » Nov 04, 2017 5:59

paul doe wrote:Yo, and the metaball demo you were coding? Was it not one of these 'coding challenges'? =D

It has been done before in FreeBASIC,
3d version,
viewtopic.php?f=7&t=10998&hilit=metaballs
2d version,
viewtopic.php?f=7&t=22957&hilit=metaballs
The older examples do not compile with the current FB compiler.
I haven't had time yet to get back to my version.
The 'metaball' demo I coded is about as basic OOP as it gets (no pun intended, seriously) but you already stated that you couldn't understand a iota of it =D

If you mean the Frame rate independent game loop example I did assume the metaballs code was buried in there somewhere I was however thinking in terms of a short simple single file code challenge solution although included files with functions for general use would be acceptable .
Ok I just had another look at your code. The metaballs and player are derived from IEntity object but given their own stuff? All a bit hard for me at this time. I think the utube example was written in JavaScript and it seemed to use classes but it looked a hell of a lot simpler than your version. Maybe a simple version is not possible with FreeBASIC. I will have to look at my own version and see if I can OO the code.
The 3D Menger Sponge fractal can be easily coded in FB, you even have the means to do so
EDIT: Or perhaps it was a sample of how easy it is to do 3D coding in the processing environment? I'm not too bright today, sorry mate =D

Still need to digest your 3d stuff and the utube example is rendered in its final version. The utube examples are built up piece by piece. As I wrote this particular example is probably too complex for a simple FreeBASIC version as you would have to duplicate whatever is innate in the processing environment.
Coding challenges was only a suggestion and this thread will probably die or diverge into something else like all my other thoughts about how to show case FreeBASIC.
.
paul doe
Posts: 804
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Coding challenges?

Postby paul doe » Nov 04, 2017 16:09

BasicCoder2 wrote:If you mean the Frame rate independent game loop example I did assume the metaballs code was buried in there somewhere I was however thinking in terms of a short simple single file code challenge solution although included files with functions for general use would be acceptable.

Yep. This is the actual metaball rendering code (it's in 'gameLoop.bi'):

Code: Select all

sub GameLoop.render( byval lerp as single = 0.0 )   
   /'
      Renders the screen
      
      The rendering process happens in two passes, and is rather simple: first, all metaballs are
      blitted to a render context (here, it is stored in the 'm_context' member variable) using
      additive blending. Then, this context is in turn rendered to the screen, pixel by pixel.
      If the pixel value (I use the red component   in this example) is greater than a certain threshold
      (represented by the 'm_threshold' member var), we draw it using the red tint of the pixel read.
      If not, we draw it using the full value   of the pixel we read before. This creates that 'halo'
      effect you see around the metaballs   because each metaball is rendered in its own context in
      greyscale (check the drawMetaballImage() function in the FBRenderDevice definition).
   '/   
   '' set a background color
   dim as uint32 clearColor = rgba( 0, 0, 0, 0 )

   dim as integer count = m_state->entityList->count
   dim as IEntity ptr e
      
   '' clears the rendering context used for the metaballs
   m_context->clearContext( clearColor )
   
   '' first pass: render the metaballs to a separate context
   for i as uinteger = 1 to count - 1
      e = m_state->entityList->get( i )
      e->render( m_renderDevice, m_context, m_state, lerp )
   next
   
   /'
      Second pass: render the context to the screen
      
      We don't have to clear the screen, because we are going to overwrite it anyway.
   '/
   '' Gets the 'handle' of the render device, which is in fact a fb.image ptr
   dim as uint32 ptr scrp = m_renderDevice->handle
   
   /'
      Skip the header of the context, as FB needs the header to be intact to be
      able to put() the buffer later
   '/
   dim as uint32 ptr bckp = m_context->handle + sizeOf( fb.image ) \ sizeOf( uint32 ) 
   /'
      Computes the padding of each scanline
      
      This is done because, when doing low-level rendering code, you have to take into account
      that FBGFX aligns the buffer returned by imageCreate() to a multiple of 16 bytes to keep
      the buffer aligned (this allows for faster memory access). So, the amount of padding is
      simply the pitch minus the buffer width (scaled here to get the value in INTEGERS, not bytes)
   '/
   dim as integer padding = ( m_context->pitch \ m_context->bitsPerPixel ) - m_context->width
   
   '' the RGBAColor is an union, and is defined in 'math.bi'
   dim as RGBAColor c
   
   screenLock()      
      for y as integer = 0 to m_context->height - 1
         for x as integer = 0 to ( m_renderDevice->pitch \ m_renderDevice->bytesPerPixel ) - 1
            '' read a pixel from the render context
            c = *bckp
            
            '' check if the red component is above 'm_threshold' (try changing this to use the
            '' alpha component and see what happens)
            if( c.r > m_threshold ) then
               *scrp = rgb( c.r, 0, 0 )
            else
               *scrp = c      
            end if
            
            scrp += 1
            bckp += 1
         next
         
         '' skip the remaining padding pixels of the scanline
         bckp += padding
      next
      
      /'
         Renders 'player' entity to screen
         
         Note how we don't pass m_context (the rendering context where the metaballs were rendered) but
         the rendering context OF THE SCREEN. We get this context via m_renderDevice->screenContext. Look
         at the implementation of FBRenderContext for an explanation on this.
      '/
      e = m_state->entityList->get( 0 )
      e->render( m_renderDevice, m_renderDevice->screenContext, m_state, lerp )
   screenUnlock()   
end sub

So, you see, the actual metaball rendering thing its merely secondary to the demo.
BasicCoder2 wrote:Ok I just had another look at your code. The metaballs and player are derived from IEntity object but given their own stuff? All a bit hard for me at this time. I think the utube example was written in JavaScript and it seemed to use classes but it looked a hell of a lot simpler than your version. Maybe a simple version is not possible with FreeBASIC. I will have to look at my own version and see if I can OO the code.

Yes, they are. That's what the interfaces are for, to treat the two objects polymorphically , as if they were the same thing. Which, code wise, they actually are =D
I can give further insight in that thread if you wish, to avoid polluting this one.
BasicCoder2 wrote:Still need to digest your 3d stuff and the utube example is rendered in its final version. The utube examples are built up piece by piece. As I wrote this particular example is probably too complex for a simple FreeBASIC version as you would have to duplicate whatever is innate in the processing environment.

Of course it is fully rendered, you're comparing a framework with plain FB (no libraries). And it's not that the example is complex, but do you want to learn how to define a Menger Sponge in 3D, or how to render it? They are two very different topics, as you can imagine. 3D rendering, unfortunately, isn't as simple as you may think. The paper plane demo shows the basic math and concepts that you'll need to learn to be able to implement a viewing system for a 3D engine (camera, objects and the like) without the rendering code.
If we were to start with the rendering, we have to talk about normals, UV coordinates, lighting and shading, among other many topics. All this assuming, of course, that you already grasped the basics (at the very least, vectors, matrices and projections).

So, to get back on topic:
BasicCoder2 wrote:Coding challenges was only a suggestion and this thread will probably die or diverge into something else like all my other thoughts about how to show case FreeBASIC.

The idea is interesting, but remember that FB is rather 'bare bones', it isn't a framework. There are things that are bound to be more complicated (read: implemented by yourself) to be able to port the coding challenges provided =D
Last edited by paul doe on Nov 04, 2017 17:55, edited 2 times in total.
fxm
Posts: 8424
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Coding challenges?

Postby fxm » Nov 04, 2017 16:33

paul doe wrote:Yes, they are. That's what the interfaces are for, to treat the two objects polymorphically , as if they were the same thing. Which, code wise, they actually are =D

I agree.
It why I wish that FreeBASIC will be able soon to support the Interfaces as this following (for example):
    Interface IA
    .....
    End Interface

    Type A Implements IA
    .....
    End Type

    Interface IB Extends IA
    .....
    End Interface

    Type B Extends A Implements IB
    .....
    End Type
paul doe
Posts: 804
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Coding challenges?

Postby paul doe » Nov 04, 2017 16:47

fxm wrote:I agree.
It why I wish that FreeBASIC will be able soon to support the Interfaces as this following (for example):
    Interface IA
    .....
    End Interface

    Type A Implements IA
    .....
    End Type

    Interface IB Extends IA
    .....
    End Interface

    Type B Extends A Implements IB
    .....
    End Type

Yep. I use very similar semantics in the code, only I fake it with #defines =D
BasicCoder2
Posts: 3310
Joined: Jan 01, 2009 7:03

Re: Coding challenges?

Postby BasicCoder2 » Nov 04, 2017 20:04

paul doe wrote:If we were to start with the rendering, we have to talk about normals, UV coordinates, lighting and shading, among other many topics.

I entered the water to test its depth and whoops over my head :)

All this assuming, of course, that you already grasped the basics (at the very least, vectors, matrices and projections).

Yes I have occasionally read about all that but it never came together as a working understanding. So maybe my trying to do 3D from the ground up is too big an ask. Clearly there are ways of making the 3D programming simpler with functions such are being used in the processing environment even if the programmer does not know the mechanics within those functions.
.
paul doe
Posts: 804
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Coding challenges?

Postby paul doe » Nov 04, 2017 20:31

BasicCoder2 wrote:
paul doe wrote:If we were to start with the rendering, we have to talk about normals, UV coordinates, lighting and shading, among other many topics.

I entered the water to test its depth and whoops over my head :)

So it seems =D
BasicCoder2 wrote:Yes I have occasionally read about all that but it never came together as a working understanding. So maybe my trying to do 3D from the ground up is too big an ask. Clearly there are ways of making the 3D programming simpler with functions such are being used in the processing environment even if the programmer does not know the mechanics within those functions.

Of course. But there's a problem with that: you learn nothing.

I distinctly remember when OpenGL switched from a fixed function pipeline to a programable one. Many folks were basically left stranded, for they grow so accustomed to do things the 'easy' way that they really didn't have any idea how those things were done 'under the hood' (many of them had programmed their '3D Engines' without ever having touched a single line of 3D code LOL). That's why I fully supported your initiative of trying to do 3D without OpenGL, but only from the 'low level' perspective (the maths involved). Once they're correctly understood, you can make a better, more useful abstraction on top of them (indeed, a 3D engine)

There are no shortcuts on learning, unfortunately =D
BasicCoder2
Posts: 3310
Joined: Jan 01, 2009 7:03

Re: Coding challenges?

Postby BasicCoder2 » Nov 04, 2017 21:08

paul doe wrote:But there's a problem with that: you learn nothing.


A long long time ago I remember using a cpu called the Z80 without any math co processor. Articles came out in electronic magazines (for that is where hobby computing started) about how to compute say the TAN of an angle. Now we use a co processor or at a high level just call a TAN routine. I see it as using higher level modules which in turn are made out of lower level modules all the way down to the hardware. You can choose your "level of understanding" according to what is available. Want to design and build your own kitchen layout? You could learn how to make cabinets or you could simply use premade and predrilled modules and arrange them how you like. Ok you will not learn how to make cabinets from scratch but you will have a nice working kitchen.

Yes I was interested in what took place under the hood. It is cool to write your own TAN routine if you know how. But the reality is it takes time and the reasons some people can get something up and running quickly is because they use other people's code without understanding what is under the hood. For example I wanted to build from the ground up a robot like Loki using FreeBASIC. Now Dave Shinsel did not program from the ground up he used other people's modules for 3d vision, speech and so on ... He has a working robot I don't.
http://www.dshinsel.com/

When I first looked at doing 3d I simply imagined it as involving angles. Rotate your eyes around left right and rotate your eyes up and over. The first was implemented in the little robot running about a world of posts. It was of course a fish eye 360 view so I added a window into the view. With the ray caster I did the same ending up with curved walls because of the fish eye affect until someone explained the simple one line math involved. Again I imagined doing the same in the up and over view but never worked it out.

So it is not that I am not interested in what goes on under the hood I just haven't been able to maintain the concentration or have the time to work it out. At some point you might say, I want some nice kitchen cabinets and it would be cool to chop down some trees and make it from scratch but then there is so many other things I would like to do and indeed have to do so perhaps modular kitchen cabinets will have to be used.

It is not a case of learning nothing it is a case of learning at a higher level how to get something done.

.
paul doe
Posts: 804
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Coding challenges?

Postby paul doe » Nov 04, 2017 21:22

Yeah, I get your point. But mine isn't so much about reinventing the wheel but more like learning the ropes ;)
BasicCoder2 wrote:When I first looked at doing 3d I simply imagined it as involving angles

Haha I wished, too ;) But, unfortunately, seems like the more deceptively simple something looks, the more complicated it really is =D
BasicCoder2
Posts: 3310
Joined: Jan 01, 2009 7:03

Re: Coding challenges?

Postby BasicCoder2 » Nov 05, 2017 5:09

BasicCoder2 wrote:I entered the water to test its depth and whoops over my head :)
Paul doe replied: So it seems =D
===========
Just spent some time reading the links you gave in the 3d without opengl thread. Starting to get how to translate matrix representations to code.
It is really like a gigantic jigsaw puzzle where I am only seeing parts of it and until I insert the last piece I doubt I will see the whole picture.
Although I don't know if i will keep at it I have gone back to that thread and am going through the 2d version you started with.
Dodicat mentioned matrix multiplication is too slow. I had hoped to use his short code to make more complex 3d scenes with objects but modifying code without understanding makes it hard to debug. Maybe eventually I will get the key concepts and all will become clear or maybe even obvious.
.
paul doe
Posts: 804
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Coding challenges?

Postby paul doe » Nov 05, 2017 5:43

BasicCoder2 wrote:Just spent some time reading the links you gave in the 3d without opengl thread. Starting to get how to translate matrix representations to code.
It is really like a gigantic jigsaw puzzle where I am only seeing parts of it and until I insert the last piece I doubt I will see the whole picture.

Good. Lookey what I've found, digging deep into my documents:
https://github.com/glasyalabolas/3D-basics
That's the original tutorial that I mentioned. It uses 3x3 matrices, but don't worry about it now. It's written in C++, but is rather easy to follow. The order I'd recommend would be Vectors, Matrices and Transformations, and lastly the 3D Basics article, as it's the natural order in which you need to build the knowledge. Hope you find it useful.
BasicCoder2 wrote:Although I don't know if i will keep at it I have gone back to that thread and am going through the 2d version you started with.

That's ok. Do take notice, however, that the 2D demos actually use 3x3 matrices, that are treated like 2D homogeneous matrices. Just pretend that the third component (the w one) doesn't exist for now =D
BasicCoder2 wrote:Dodicat mentioned matrix multiplication is too slow. I had hoped to use his short code to make more complex 3d scenes with objects but modifying code without understanding makes it hard to debug. Maybe eventually I will get the key concepts and all will become clear or maybe even obvious.

Don't worry about slowness yet. Just focus on getting the concepts right. Once you're able to 'put the puzzle together', dodicat's code (and also dafhi's) will make a lot more sense to you. Just keep pushing. Or sludging =D
BasicCoder2
Posts: 3310
Joined: Jan 01, 2009 7:03

Re: Coding challenges?

Postby BasicCoder2 » Nov 05, 2017 7:46

What exactly is a Scaler?
There are some broken links in the article.
It appears SDL is being used for the graphics.
http://lodev.org/cgtutor/
I was using DevCpp and had just managed to install SDL when I decided to move over to FreeBASIC.
Recently I downloaded Code::Blocks but was unable to install SDL so gave up on it.
In mat3.bi what is the purpose of,
declare operator cast() as string
=====================================================
Although your program worked before I now just got a compiler error.
Command executed:
"C:\FreeBasic\fbc.exe" "C:\FreeBasic\programs\my3d\paulDoe3d\main.bas"

Compiler output:
C:\FreeBasic\bin\win32\ld.exe: cannot open output file C:\FreeBasic\programs\my3d\paulDoe3d\main.exe: Permission denied

Results:
Compilation failed

System:
FBIde: 0.4.6
fbc: FreeBASIC Compiler - Version 1.05.0 (01-31-2016), built for win32 (32bit)
OS: Windows NT 6.2 (build 9200)

==================================================

Had the blue screen of death a few days ago and had to buy a new computer. Now on the new computer I can't delete the paulDoe3d folder as it keeps giving me a message that I need permission from LAPTOP-EEPKB59P\Smith to make changes to the folder!! Doesn't explain how to get that permission. I was going to delete the folder and reinstall the files to see if it fixed the compiler error.
===================================================
Just spent the last few hours trying to fix the delete folder issue without success however other folders seem unaffected so I made a new folder and started all over again and it works ok from the new folder. Now to spend the rest of the week figuring out the code.
.
dodicat
Posts: 5251
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Coding challenges?

Postby dodicat » Nov 05, 2017 13:19

The cast operator to string provides a convenient way of printing the matrix
Of course, you have to lay out the string in your own way.

You can also create your own function for printing the matrix.

Code: Select all


'PAUL DOE

type mat3
      public:
         as double a, b, c
         as double d, e, f
         as double g, h, i
     
     
      declare operator cast() as string
      declare function ShowAsString() as string'another way
   end type
   
 function mat3.ShowAsString() as string
     return( _
         "[" & trim( str( a ) ) & "][" & trim( str( b ) ) & "][" & trim( str( c ) ) & "]" & chr( 10 ) & chr( 13 ) & _
         "[" & trim( str( d ) ) & "][" & trim( str( e ) ) & "][" & trim( str( f ) ) & "]" & chr( 10 ) & chr( 13 ) & _
         "[" & trim( str( g ) ) & "][" & trim( str( h ) ) & "][" & trim( str( i ) ) & "]" )
end function

 operator mat3.cast() as string export
      '' this is only to obtain a human readable representation of the matrix
      return( _
         "[" & trim( str( a ) ) & "][" & trim( str( b ) ) & "][" & trim( str( c ) ) & "]" & chr( 10 ) & chr( 13 ) & _
         "[" & trim( str( d ) ) & "][" & trim( str( e ) ) & "][" & trim( str( f ) ) & "]" & chr( 10 ) & chr( 13 ) & _
         "[" & trim( str( g ) ) & "][" & trim( str( h ) ) & "][" & trim( str( i ) ) & "]" )
   end operator
   
   
   screen 19
   
   dim as mat3 m
   print m  'using cast
   print
   print
   print m.ShowAsString 'custom method
   sleep
 
   
   

You seem to have the same OS as me.
Win 10
Tips:
Don't use program files for any of your own apps (FreeBASIC e.t.c.)
I use either a folder on C: or a folder on the desktop.

Don't install anything by an executable file, always get a .zip file, and if possible always get hold of a portable app rather than a full blown app (Dev cpp for instance)

Program Files seems to be blocked in Win 10.
(For myself, too much hassle to unblock it, I just avoid it)

Many things don't work well on Win 10.
Fbedit is an example.
paul doe
Posts: 804
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Coding challenges?

Postby paul doe » Nov 05, 2017 14:41

dodicat wrote:Many things don't work well on Win 10.
Fbedit is an example.

?
That's odd. I have Win10 too. I use FBEdit and so far, had no trouble at all =?

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest