Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Weather Tutorial (Using DrawText for Snow/Rain)


Rob Janes
 Share

Recommended Posts

This tutorial will let you add weather, (Snow/Rain) to your Eclipse Engine.  It will require modifications both Client and Server Side. 

Let's get started shall we?

**1\. Server Side Changes**

We need to declare Weather on the server, this can be done with a string, integer, just something to identify what the current weather is, I use a string as I use the 'name' of the weather to send to players later.

In modTypes add
```
Public Weather As String
```
in modEnumerations add this to the server packets.
```
SSendWeather
```
Add this new Sub into modServerTCP
```
Sub SendWeather(ByVal index As Long)
Dim buffer As clsBuffer
    Set buffer = New clsBuffer
    buffer.WriteLong SSendWeather
    buffer.WriteString Weather
    SendDataTo index, buffer.ToArray()
    Set buffer = Nothing
    Call PlayerMsg(index, "Current Weather: " & Weather, BrightCyan)
End Sub
```
Add 3 Buttons on frmMain, called cmdSun, cmdRain and cmdSnow, these are the commands to change the Weather on dynamically through the Server.

```
Private Sub cmdRain_Click()
Weather = "Rain"
Dim I As Integer
For I = 1 To MAX_PLAYERS
    If IsPlaying(I) Then
        Call SendWeather(I)
    End If

Next I
End Sub

Private Sub cmdSnow_Click()
Weather = "Snow"
Dim I As Integer
For I = 1 To MAX_PLAYERS
    If IsPlaying(I) Then
        Call SendWeather(I)
    End If

Next I
End Sub

Private Sub cmdSun_Click()
Weather = "Sun"
Dim I As Integer
For I = 1 To MAX_PLAYERS
    If IsPlaying(I) Then
        Call SendWeather(I)
    End If

Next I

End Sub
```
Add this into modPlayer, Sub JoinGame after SendWelcome
```
'Send Weather
    Call SendWeather(index)
```
This concludes server-side adjustments

–--------------------------------------------------------------------------------
**2\. Client Side Adjustments**

First we are going to use a new Font for the Weather so in modFonts just after it declares ChatFont add
```
Public WeatherFont As Long
```
We are going to add some declarations now, I used a custom module but you can add it anywhere at the beginning of existing modules.
```
Public Const WEATHER_DROPS = 250
Public Const SUN = 1
Public Const RAIN = 2
Public Const SNOW = 3
Public Type DropData
    x As Integer
    y As Integer
    strText As String
End Type
Public Drop(1 To WEATHER_DROPS) As DropData
Public Weather As Integer
```
We're now going to add Subs to initilize the Weather system, and also a custom RandomNumber Function. Add this to any module, again, I used a custom module.
```
Function RandomNumber(Lowerbound As Long, Upperbound As Long)
Randomize
RandomNumber = Int(Rnd * Upperbound) + Lowerbound
End Function

Public Sub InitWeather()
Dim I As Integer
For I = 1 To WEATHER_DROPS
Drop(I).x = RandomNumber(1, 800)
Drop(I).y = RandomNumber(1, 600)
Next I

End Sub
```
Add a Timer on frmMain, with an Interval of 50 and call it tmrUpdateWeather
```
Dim I As Integer
For I = 1 To WEATHER_DROPS
    Drop(I).y = Drop(I).y + Int(RandomNumber(1, 5))
    If Drop(I).y > 600 Then
    Drop(I).y = 0
    Drop(I).x = Int(RandomNumber(-100, 900))
    End If
    If Weather = RAIN Then
    Drop(I).x = Drop(I).x + Int(RandomNumber(1, 1))
    End If

Next I
If Weather = SNOW Then
tmrUpdateWeather.Interval = 50
End If
If Weather = RAIN Then
tmrUpdateWeather.Interval = 20
End If
```
In modText, in the SetFont Mod, we are now going to define the font we use for the weather
```
WeatherFont = CreateFont(24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Arial")
```
Still in modText, add the following subs
```
' GDI text drawing onto buffer
Public Sub DrawWeather(ByVal hdc As Long, ByVal x, ByVal y, ByVal text As String, color As Long)
    Call SelectObject(hdc, WeatherFont)
    Call SetBkMode(hdc, vbTransparent)
    Call SetTextColor(hdc, 0)
    Call TextOut(hdc, x + 1, y + 1, text, Len(text))
    Call SetTextColor(hdc, color)
    Call TextOut(hdc, x, y, text, Len(text))
End Sub
Public Sub DrawRain()
Dim DropID As Integer

For DropID = 1 To WEATHER_DROPS
Drop(DropID).strText = "'"
Call DrawWeather(TexthDC, Camera.Left + Drop(DropID).x, Camera.top + Drop(DropID).y, Drop(DropID).strText, &HFFFFC0)
Next DropID

End Sub
Public Sub DrawSnow()
Dim DropID As Integer
For DropID = 1 To WEATHER_DROPS
Drop(DropID).strText = "*"
Call DrawWeather(TexthDC, Camera.Left + Drop(DropID).x, Camera.top + Drop(DropID).y, Drop(DropID).strText, vbWhite)
Next DropID

End Sub
```
In modGeneral, sub Main, we need to get the Weather 'engine' going.  Add this to the start of Sub Main
```
'Set Default Weather to Sun
    InitWeather
    Weather = SUN
```
In modDirectDraw7 under the Render Graphics Sub, Add the following just after it defines TexthDC
```
If Weather = RAIN Then
      DrawRain
    End If

    If Weather = SNOW Then
        DrawSnow
    End If

```
In modEnumerations add this to the ServerPackets
```
SSendWeather
```
In modHandleData, in the InitMessages Sub, add this at the bottom.
```
HandleDataSub(SSendWeather) = GetAddress(AddressOf HandleWeather)
```
In modHandleData add the new Sub
```
Private Sub HandleWeather(ByVal Index As Long, ByRef Data() As Byte, ByVal StartAddr As Long, ByVal ExtraVar As Long)

    Dim Buffer As clsBuffer
    Dim strWeather As String

    Set Buffer = New clsBuffer
    Buffer.WriteBytes Data()
    strWeather = Buffer.ReadString
    Set Buffer = Nothing

    If strWeather = "Sun" Then
    Weather = SUN
    End If
    If strWeather = "Rain" Then
    Weather = RAIN
    End If
    If strWeather = "Snow" Then
    Weather = SNOW
    End If

End Sub
```
End of Client Side Changes
–--------------------------------------------

Once you compile, you will now be able to click the new buttons on your server and change the weather!
Link to comment
Share on other sites

  • Replies 53
  • Created
  • Last Reply

Top Posters In This Topic

You do realise that drawing text is one of the worst things you can do to slow down your game? I'm being serious. Do an FPS test before you turn on your rain/snow then do an FPS test after. With the lock off. There's a reason I try and minimise text rendering, to the point where I'm actually considering a bitmap font or pre-rendering to surfaces.

BltFast would be 100x easier to do and 1000x faster in practice.
Link to comment
Share on other sites

Indeed, it does drop the FPS quite a bit, but NOT to a point where it drops below the locked 64fps anyways, so it's a moot point from that stand point.  You could reduce the number of 'drops' if it was drastically hindering your FPS.  Though you are 100% correct, there are MUCH FASTER ways to do this.
Link to comment
Share on other sites

@SamuGames:

> Indeed, it does drop the FPS quite a bit, but NOT to a point where it drops below the locked 64fps anyways, so it's a moot point from that stand point.

I suggest you don't dismiss things as a 'moot point' just because they don't have an impact on your performance. That's your machine. Imagine one of your players is getting a much lower FPS. Around 100\. They'll get the exact same performance drop as you, taking them well below the 64 lock.

You want to create the game to the lowest denominator, not tailored to your build.
Link to comment
Share on other sites

I agree with the fact that it won't make much of a difference currently. Though as robin said if they're is a better method you might as well take it. This method works fine but as I found out making Eclipse++ taking the quick way out does not work. It's fine when it's one feature but eventually you get bogged down with a ton of features that are improperly coded that together really lag the game.
Link to comment
Share on other sites

Noted Robin, and I agree Marsh, however I truly don't imagine people to be running anything less than 1-2GB RAM, with atleast 2Ghz Processor and a 256MB video card. ;) That's what Recommended and Minimum System Requirements listed on the site will be for.  No other features at this point really have an impact on the 'rendering engine' so it shouldn't be an issue, though I may end up re-writing this and putting out a new tutorial.  I'd be interested to hear what FPS people are getting with this tutorial.
Link to comment
Share on other sites

@SamuGames:

> It wouldn't be difficult to simply change the current code around so it still uses the same Drop Array, and rather than using DrawText it simply Blts the Snow or Rain graphic at the proper screen position.  A minor tweak actually.

Hence why I suggested it. ;]
Link to comment
Share on other sites

Don't assume players will meet the requirements to play your game. After running Ambardia for 4 years on a dedicated 24/7 server I was still always amazed at how many people still have crappy systems. Also a lot of my players were starting to come from south america and india and almost all of them had less than 1ghz of ram (most in fact were running P2' and 3's with 256 megs of ram.
Link to comment
Share on other sites

@Ambard:

> Don't assume players will meet the requirements to play your game. After running Ambardia for 4 years on a dedicated 24/7 server I was still always amazed at how many people still have crappy systems. Also a lot of my players were starting to come from south america and india and almost all of them had less than 1ghz of ram (most in fact were running P2' and 3's with 256 megs of ram.

I'd love to see EE running on that kind of machine. xD

EO could probably pull it off. Even more so once I start pre-rendering the maps.
Link to comment
Share on other sites

@Ambard - That's why "Minimum" requirements are posted and support would be limited for people not meeting minimum requirements.  True though a lot of people still use slower machines.  I've updated the rendering to use images and it only drops FPS by ~20 now.
Link to comment
Share on other sites

  • 2 weeks later...
~~question can we add more then just rain/snow/sun using this? if so I want to add a few more then just the basic's.~~

nvm.. I think I know how I can . I will have to add more code then just the ones you have but I can get it working probly.tho I probly can use yours as a base example code.

yup I manage to get it working :P tho adding mubital effects is kind of hard and one of the weather effects I want to do consist of that. so any subjestions on how I can do that without it overlaping the other effects also is ~~there a color chart I can follow?~~

also I notice on when your  changing the color you can type like Red & and yellow to get orange. and stuff like that . I been messing around with it :P now if I can just figure out a way to change one character to another.
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×
×
  • Create New...