Author Topic: Checkered Floor Pattern  (Read 9924 times)

0 Members and 1 Guest are viewing this topic.

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Checkered Floor Pattern
« on: June 06, 2011 »
Hi all, I have a png file that is a checkered floor pattern this is then drawn in my main loop and I was wondering if it would be better to draw the checkered floor at run time and not use a png file at all the only problem I have is I am not sure haw to do it, a two dimensional array to hold the X & Y cords but can't work out how to have one white one black etc..
Any help with this would be great
Thanks

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: Checkered Floor Pattern
« Reply #1 on: June 06, 2011 »
You could check if the X value is a power of 2 to determine if it's black or white. Not sure what language you're using, but I'm sure it has a modulus (mod) operator to tell if a number is exactly divisible by another number. Like so:

if X mod 2 = 0 (even)

That will only give you alternating black and white blocks.  You'll need to start from black on one row and white on the next and then black on the next etc etc. You could probably use the mod method again and just have a nested loop to check the Y value:

if Y mod 2 = 0 (even)
  startColour = black
else
  startColor = white
end if

There are probably a load of other ways to do it, but that's probably how I'd approach it.
raizor

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Checkered Floor Pattern
« Reply #2 on: June 06, 2011 »
I'd do what Raizor said, but some of the other approaches you could take.

Draw 2 tiles,1 black and one white.

  Color_Start=1
  for y=0 to yres step tile_height
   Color = Color_Start
   Color_Start=Color_Start+1
     if color_Start>2 then Color_Start=1
     for x=0 to xres step tile_width
      color=color+1
      if color>2 then color =1
      if color =1 then draw tile1,x,y
      if color = 2 then draw tile2,x,y
     next x
  next y


Or simplify things and just draw a small 2*2 grid and repeat it with your X+Y loop.

Either way, if you don't have any luck, please post your source and your graphics.

Thanks
« Last Edit: June 06, 2011 by Shockwave »
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Re: Checkered Floor Pattern
« Reply #3 on: June 06, 2011 »
Thanks guys, the checked floor will have to be big up front and getting smaller to the back so it looks like a floor effect so the rect size's will change I think not sure :)

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Checkered Floor Pattern
« Reply #4 on: June 06, 2011 »
You're looking to make a 3D one?
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Re: Checkered Floor Pattern
« Reply #5 on: June 06, 2011 »
Here I have uploaded a png file for you to look at, if thats ok?

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Checkered Floor Pattern
« Reply #6 on: June 07, 2011 »
I see :)

Ok, there are several ways that you can do this but whatever way you do it you'll be better off in doing it in polygons.  I have to go to work right now but I can see that Blitzmax 2d library has a drawpoly command so I'll have a go at that if this is still not solved for you by the time I get home tonight.

What you'll be doing essentially is having a grid defined in an array of x,y and z co-ordinates, doing s simple transformation on the x+y by dividing them by the z's and just drawing polygons between them.

If you want to achieve a nice gradient like the png you posted you'd get tthe result quite quick using opengl and fog - I suspect that you're going to be combining some elements though because of the scroll question you posted?  So if that's the case we'll stick with the Max2d lib.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Re: Checkered Floor Pattern
« Reply #7 on: June 07, 2011 »
 :carrot: Sounds good to me I kind off guest it would be an Array but not to sure about the Z cord thing if you could point me in the right direction I would be most greatfull.
Thanks
« Last Edit: June 07, 2011 by Pot Noodle »

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Checkered Floor Pattern
« Reply #8 on: June 07, 2011 »
Before you look at the code, I must apologise for it - it's the second thing I've ever written in Blitz Max in my life and to be honest with you I don't get along very well with the IDE so it's not my favourite language..

Anyway, the problem you've posed is not difficult really but you need to be able to thing of things in terms of 3D.
If you look at the picture you posted, it's made up of chequered squares which are defined by their corners.
A TV screen can only display things in two Dimensions, Horizontal and Vertical.

This presents a problem when you want to draw something in 3D because the screen is 2D.

You get around the problem by computing your chequerboard / cube / whatever you want to draw in 3D and then converting those 3D co-Ordinates into 2D ones.

So for your Chequerboard, we need a grid made up of X,Y,Z Co-Ordinates.

Point your right hand index finger at your computer screen, point your right thumb up and point your right middle finger to the left.

These are your 3 Axis, your index finger is Z (depth), thumb is Y axis (vertical), middle finger is X Axis (across).

The points on the grid are defined like this and basically to convert them to screen co-ordinates, X is divided by Z and Y is divided by Z. This gives the appearence of perspective, the object appears to shrink as it disappears into the distance.

The movement in the example just scrolls forward 2 squares and flips back and the lightsourcing is faked.

It's a pretty shit example, but 3D is not the easiest thing to tackle if you're new - so at least you can just grab this code and use it if you like.

Code: [Select]
Strict

' ******************************************************************************************
' *                                                                                        *
' * 3D Chequerboard By Shockwave : www.Dbfinteractive.com                                  *
' * Apologies for this bodged code.. It's just thrown together to help someone out..       * 
' *                                                                                        *
' ******************************************************************************************

' *********************************************************************
' * Open Screen                                                       *
' *********************************************************************

SetGraphicsDriver GLMax2DDriver()

Const xres = 800
Const yres = 600

Const halfX = 400
Const halfY = 250

Graphics xres,yres,32,60,GRAPHICS_BACKBUFFER
Const Grid = 30 : Int
Const Size = 150 : Int

Global GridX : Double[Grid,Grid]
Global GridY : Double[Grid,Grid]
Global GridZ : Double[Grid,Grid]

Global Move : Double

Move = 0.0
SetGrid()

' *********************************************************************
' * Main Loop                                                         *
' *********************************************************************


Repeat

SetColor (255,255,255)

DrawGrid()
Move = Move + .025
If Move >= .5 Then Move =Move -.5
Flip(-1)
Cls

Until KeyDown(KEY_ESCAPE)

EndGraphics
End

' *********************************************************************
' * Functions                                                         *
' *********************************************************************

Function DrawGrid()

Local Z : Int
Local X : Int

Local TX : Int
Local TY : Int

Local Clr     : Int
Local Shade   : Int
Local ClrStrt : Int

ClrStrt=1

Local PolyTransform : Float [8]

For Z = 0 To Grid-2

ClrStrt=ClrStrt+1
If ClrStrt>2 Then ClrStrt=1
Clr = ClrStrt
For X = 0 To Grid-2

Clr=Clr+1
If Clr>2 Then Clr=1

TX = (GridX[X,Z]     / (GridZ[X,Z]-Move))+HalfX
TY = (GridY[X,Z]     / (GridZ[X,Z]-Move))+HalfY

Polytransform[0]=TX
Polytransform[1]=TY

TX = (GridX[X+1,Z] / (GridZ[X+1,Z]-Move))+HalfX
TY = (GridY[X+1,Z]     / (GridZ[X+1,Z]-Move))+HalfY

Polytransform[2]=TX
Polytransform[3]=TY

TX = (GridX[X+1,Z+1] / (GridZ[X+1,Z+1]-Move))+HalfX
TY = (GridY[X+1,Z+1] / (GridZ[X+1,Z+1]-Move))+HalfY

Polytransform[4]=TX
Polytransform[5]=TY

TX = (GridX[X,Z+1] / (GridZ[X,Z+1]-Move))+HalfX
TY = (GridY[X,Z+1] / (GridZ[X,Z+1]-Move))+HalfY

Polytransform[6]=TX
Polytransform[7]=TY

shade=(ty/4)-75
If shade<0 Then shade=0

If Clr =1 Then SetColor(Shade,Shade,Shade)
If Clr =2 Then SetColor( 0, 0,Shade)


DrawPoly(Polytransform)

Next

Next


End Function


Function SetGrid()


Local Xpos : Double
Local Zpos : Double
Local Ypos : Double
Local Jump : Double
Local Xscale : Double


Local Z : Int
Local X : Int

Jump = Size / Grid

Xscale = 25
Ypos = 400
Zpos = 8.4

For Z = 0 To Grid -1

Xpos = -((Size*Xscale) /2)

For X = 0 To Grid -1

GridX[X,Z] = Xpos
GridY[X,Z] = Ypos
GridZ[X,Z] = Zpos

Xpos = Xpos + (Jump*Xscale)

Next

Zpos = Zpos - (Jump/20)

Next

End Function
« Last Edit: June 07, 2011 by Shockwave »
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Re: Checkered Floor Pattern
« Reply #9 on: June 07, 2011 »
Wow .. What more can I say I just wish I understood it all Thanks so much, your right about the sinewave they are for the same project would I be better off using Darkbasic or something else?

Offline Hotshot

  • DBF Aficionado
  • ******
  • Posts: 2114
  • Karma: 91
    • View Profile
Re: Checkered Floor Pattern
« Reply #10 on: June 07, 2011 »
Stick with Blitz Max as it is much better than Darkbasic but everybody got Different View!

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Checkered Floor Pattern
« Reply #11 on: June 07, 2011 »
would I be better off using Darkbasic or something else?

I've seen this question posed a few times and it always gets a range of replies, if you were looking to develop your skills into the commercial sector then you'd probably be advised to learn something else but these days I think that a programming language is a matter of personal choice, especially if you're just doing it for your own satisfaction.

Don't be put off by thinking you've got the wrong tools for the job because Blitzmax is certainly capable of making anything you want.  It is a competant language, I am just used to using different things that's all.

The thing is that you're at a stage that I quite envy.  Ideas are still fresh to you and you just want to make them because you like the way that they look.

Stick with what you're doing and don't worry about posting questions here, we can help you with almost anything you want to throw at us.

Also the 3D stuff, well, there are easier examples of learning it than a 3D chessboard, I'd suggest that you start off with learning how to draw stuff and then start off with a simple 3D starfield and work your way through all the 3d effects :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Re: Checkered Floor Pattern
« Reply #12 on: June 08, 2011 »
Thanks for your advice I have written a lot of programs in visual basic over the years but it is windows based I have never seen an Intro or Demo done with it to many API's for a start so I will stick with Bm after all my main problem is the lack of ideas and math cells  ;D but thanks.

Offline TinDragon

  • Pentium
  • *****
  • Posts: 644
  • Karma: 24
    • View Profile
    • J2K's blog
Re: Checkered Floor Pattern
« Reply #13 on: June 08, 2011 »
Blitzmax is fine, much better in all ways than darkbasic, the downside as with all blitz languages is the basic functionallity ide, which if you prefer a more modern or visual studio style ide then I recomend blide, it has a free version which you can use for any non-commercial project.

http://www.blide.org/

Personally I use the default ide as I like the simple text editor approach, but then I dont tend to work on massive projects ;)

Dont think I have a working checkboard routine in bmax atm and I just cleaned my old bb code off my drives or I could probably have quick converted it. If I get some time later will see if I can sort one.

Offline Hotshot

  • DBF Aficionado
  • ******
  • Posts: 2114
  • Karma: 91
    • View Profile
Re: Checkered Floor Pattern
« Reply #14 on: June 08, 2011 »
Blide IDE is good  much better than BlitzMax IDE!

Cool Checkboard you got there :) 8)

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Re: Checkered Floor Pattern
« Reply #15 on: June 08, 2011 »

Offline boogop

  • C= 64
  • **
  • Posts: 73
  • Karma: 42
    • View Profile
Re: Checkered Floor Pattern
« Reply #16 on: June 16, 2011 »
That was a good question, OP. It's an ancient effect for which not a lot of source exists on Hornet (dunno about Pouet). For IntroFX (iPhone, obj-c) I totally cheated by mapping a checkerboard texture onto a rectangle, skewing the perspective, then modifying the textures X and Y coords so it would move. Doing it in raw code would be much better.
Challenge Trophies Won: