### 1. Simple Raylib Example

Hello All,

I got a little bored, so I modified the bouncing ball example. How with two bouncing balls and collision detection between them! All you need to run this example is Euphoria the Eurayib wrapper.

```without warning

include std/machine.e
include std/convert.e

include flags.e
include Euraylib.ew

atom w = 800, h = 450

InitWindow(w,h,"Bouncing Ball")

atom ball_x = rand(GetScreenWidth()) / 2
atom ball_y = rand(GetScreenHeight()) / 2
atom ball_speed_x = 5.0
atom ball_speed_y = 4.0

atom ball2_x = GetScreenWidth() / 2
atom ball2_y = GetScreenHeight() / 2
atom ball2_speed_x = 6.0
atom ball2_speed_y = 5.0

integer paused = 0
integer frameCounter = 0

SetTargetFPS(60)

atom b = bytes_to_int({0,0,0,0})
atom g = bytes_to_int({0,255,0,255})
atom r = bytes_to_int({255,0,0,255})

while not WindowShouldClose() do

if IsKeyPressed(KEY_SPACE) and paused = 0 then
paused = 1
elsif IsKeyPressed(KEY_SPACE) and paused = 1 then
paused = 0
end if

if paused = 0 then
ball_x += ball_speed_x
ball_y += ball_speed_y
ball2_x += ball2_speed_x
ball2_y += ball2_speed_y

ball_speed_x *= -1.0
ball_speed_y *= -1.0
end if

ball2_speed_x *= -1.0
ball2_speed_y *= -1.0
end if

ball_speed_x *= -1.0
ball_speed_y *= -1.0
ball2_speed_x *= -1.0
ball2_speed_y *= -1.0
end if
end if
frameCounter += 1

BeginDrawing()

ClearBackground(b)

if paused = 1  and frameCounter / 30 then
DrawText("PAUSED",1,20,30,g)
end if

DrawFPS(1,1)

EndDrawing()

end while

CloseWindow()
```

### 2. Re: Simple Raylib Example

For fun, I translated this to run under EuGTK in a window. Uses Cairo for the graphics:

You'll note that there is an unnatural result when the two collide. Someone want to fix that?

Two things I'd like to do:

1. Add more balls (the program is structured to make this easy)
2. Figure out how to check for collisions when more than 2 are bouncing! (not easy, I suspect!)

### 3. Re: Simple Raylib Example

irv said...

For fun, I translated this to run under EuGTK in a window. Uses Cairo for the graphics:

You'll note that there is an unnatural result when the two collide. Someone want to fix that?

Two things I'd like to do:

1. Add more balls (the program is structured to make this easy)
2. Figure out how to check for collisions when more than 2 are bouncing! (not easy, I suspect!)

That was fun to look at the GTK example. The collision detection in mine isn't perfect, but it works. It would be very easy to add more balls and maybe even make a little small little game out of it.

### 4. Re: Simple Raylib Example

Comparing two objects for collision is easy, but how to efficiently compare many objects, any two (or more) of which could be in collision, seems like it would be tricky - and require a lot of processing.

I'm sure routines to that have already been worked out, since games do this all the time.

Is Euphoria fast enough to do the job?

### 5. Re: Simple Raylib Example

irv said...

Comparing two objects for collision is easy, but how to efficiently compare many objects, any two (or more) of which could be in collision, seems like it would be tricky - and require a lot of processing.

I'm sure routines to that have already been worked out, since games do this all the time.

Is Euphoria fast enough to do the job?

Well I'm sure Euphoria would be fast enough. I've already added another ball and it still runs fine. I'm sure you could keep adding balls until the program slows down significantly.

NOTE: Ignore the color changing ball for now, that was just for a test. If you want to add or improve upon it, feel free.

```without warning

include std/machine.e
include std/convert.e

include flags.e
include Euraylib.ew

atom w = 800, h = 450

InitWindow(w,h,"Bouncing Ball")

atom ball_x = rand(GetScreenWidth()) / 2
atom ball_y = rand(GetScreenHeight()) / 2
atom ball_speed_x = 5.0
atom ball_speed_y = 4.0

atom ball2_x = GetScreenWidth() / 2
atom ball2_y = GetScreenHeight() / 2
atom ball2_speed_x = 6.0
atom ball2_speed_y = 5.0

atom ball3_x = rand(GetScreenWidth()) / 2 - 3
atom ball3_y = rand(GetScreenHeight()) / 2 - 3
atom ball3_speed_x = 8.0
atom ball3_speed_y = 6.0

integer paused = 0
integer frameCounter = 0

SetTargetFPS(60)

atom b = bytes_to_int({0,0,0,0})
atom g = bytes_to_int({0,255,0,255})
atom r = bytes_to_int({255,0,0,255})
atom blu = bytes_to_int({0,0,255,255})
atom whi = bytes_to_int({255,255,255,255})
atom yel = bytes_to_int({255,255,0,255})
atom pur = bytes_to_int({128,0,128,255})

sequence cols = {blu,whi,yel,pur,g,r}
constant MAX_COLS = 6

while not WindowShouldClose() do

if IsKeyPressed(KEY_SPACE) and paused = 0 then
paused = 1
elsif IsKeyPressed(KEY_SPACE) and paused = 1 then
paused = 0
end if

if paused = 0 then
ball_x += ball_speed_x
ball_y += ball_speed_y
ball2_x += ball2_speed_x
ball2_y += ball2_speed_y
ball3_x += ball3_speed_x
ball3_y += ball3_speed_y

ball_speed_x *= -1.0
ball_speed_y *= -1.0
end if

ball2_speed_x *= -1.0
ball2_speed_y *= -1.0
end if

ball3_speed_x *= -1.0
ball3_speed_y *= -1.0
end if

ball_speed_x *= -1.0
ball_speed_y *= -1.0
ball2_speed_x *= -1.0
ball2_speed_y *= -1.0
end if

ball2_speed_x *= -1.0
ball2_speed_y *= -1.0
ball3_speed_x *= -1.0
ball3_speed_y *= -1.0
end if

ball3_speed_x *= -1.0
ball3_speed_y *= -1.0
ball_speed_x *= -1.0
ball_speed_y *= -1.0
end if
end if
frameCounter += 1

BeginDrawing()

ClearBackground(b)

--for i = 1 to MAX_COLS do
--	DrawCircleV(10,90,20,rand(cols[i]))
--end for

if paused = 1  and frameCounter / 30 then
DrawText("PAUSED",1,20,30,g)
end if

DrawFPS(1,1)

EndDrawing()

end while

CloseWindow()

```

### 6. Re: Simple Raylib Example

For testing, I changed the 60fps timer in my program to use GIdle instead (which runs as fast as possible, whenever there's a free cpu available).

I can get 1900+ fps on my computer. That's with the relatively simple movement and collision detection. So I suspect even more complicated processing would still have plenty of time, if running at a more reasonable 60 fps (which should be fast enough for anyone).

### 7. Re: Simple Raylib Example

irv said...

Comparing two objects for collision is easy, but how to efficiently compare many objects, any two (or more) of which could be in collision, seems like it would be tricky - and require a lot of processing.

I'm sure routines to that have already been worked out, since games do this all the time.

Is Euphoria fast enough to do the job?

There are physics libraries that might be usable by a Euphoria program, but I expect Euphoria is fast enough for simple collision-detection jobs.

### 8. Re: Simple Raylib Example

irv said...

For testing, I changed the 60fps timer in my program to use GIdle instead (which runs as fast as possible, whenever there's a free cpu available).

I can get 1900+ fps on my computer. That's with the relatively simple movement and collision detection. So I suspect even more complicated processing would still have plenty of time, if running at a more reasonable 60 fps (which should be fast enough for anyone).

That's pretty good! Dang. I'm thinking of using EuGTK for a game app, but I don't like the idea of coding up my own game engine. If anybody has that passion, I'm sure Euphoria could handle it just fine, especially with the compiling option.

### 9. Re: Simple Raylib Example

Euphoria is more than capable for game development. I have wrapped several libraries that can aid in game development, such as in the example, Raylib. I have also wrapped SDL2,SFML2,Sigil, etc. Since those wrappers were wrapped using the C ports or were written in C, then compiling them should be fairly straight forward. As for physics, I have tried wrapping Chipmunk physics, but was having trouble.

### 10. Re: Simple Raylib Example

Hey, I get an error when I try to load texture using raylib. Anyone knows what is causing this problem?

```C:\Euphoria4_1\Projects\Euraylib-master\Euraylib-master\Euraylib.ew:4250 in function gLoadTexture()
```

```without warning

include std/machine.e
include std/convert.e

include Euraylib.ew
--include raylibshim.e

constant width = 800
constant height = 450

atom black = bytes_to_int({0,0,0,0})
atom white = bytes_to_int({255,255,255,255})
atom yellow = bytes_to_int({255,255,0,255})
atom blue = bytes_to_int({0,0,255,255})

InitWindow(width,height,"Simple Texture")

sequence texture_info = {3,256,256,1,0}

while not WindowShouldClose() do

BeginDrawing()

ClearBackground(blue)

DrawText("This is a Texture",1,1,20,yellow)

DrawTexture(texture_info,10, 50,white)

EndDrawing()

end while

CloseWindow()
```

### 11. Re: Simple Raylib Example

This example I wrote works and loads a texture for me.

```without warning

include std/machine.e
include std/convert.e

include Euraylib.ew
--include raylibshim.e

constant width = 800
constant height = 450

atom black = bytes_to_int({0,0,0,0})
atom white = bytes_to_int({255,255,255,255})
atom yellow = bytes_to_int({255,255,0,255})
atom blue = bytes_to_int({0,0,255,255})

InitWindow(width,height,"Simple Texture")

while not WindowShouldClose() do

BeginDrawing()

ClearBackground(blue)

DrawText("This is a Texture",1,1,20,yellow)

sequence texture_info = {3,256,256,1,0}

DrawTexture(texture_info,10, 50,white)

EndDrawing()

end while

CloseWindow()
```