Solved

VB too slow or I'm a bad programmer!

Posted on 1999-01-26
6
382 Views
Last Modified: 2012-05-04
I'm telling you right now I'm a bad programmer. I sat and looked at this code for a long time and could not speed it up. It takes a long time run this loop to it's end. It's a classic case... I need to put my tiles to the picture box. I have mutipal layers. 6 to be exact. The code is below. Please take a look and tell me what you think. Thanks.

Sub draw_map()
If xcord < 1 Then xcord = 1
If ycord < 1 Then ycord = 1
mapeditor.VScroll1.Value = ycord
mapeditor.HScroll1.Value = xcord
Dim page As Byte:  'the page the curent tile is being pulled off of
Dim Xmapshwon As Byte
Dim ymapshown As Byte
Dim xnow As Long
Dim ynow As Long
Dim temp_tile As Integer
Dim ANACLOCK_SET As Integer
Dim tilex As Integer
Dim tiley As Integer
Dim tilenow(6) As Integer
mapeditor.Picture1.Picture = LoadPicture
ANACLOCK_SET = anaclock2
'GoTo test


XMAPSHOWN = Int(375 / tile_width)
ymapshown = Int(325 / tile_Height)
tilex = xcord
tiley = ycord

For xnow = 0 To XMAPSHOWN * (tile_width + 1) Step tile_width
tilex = tilex + 1
tiley = ycord
For ynow = 0 To ymapshown * (tile_Height + 1) Step tile_Height

'this is the order of the tiles drawn
tilenow(1) = map(tilex, tiley).land1
tilenow(2) = map(tilex, tiley).land2
tilenow(3) = map(tilex, tiley).object
tilenow(4) = map(tilex, tiley).sprite
tilenow(5) = map(tilex, tiley).land3
tilenow(6) = map(tilex, tiley).land4

For tile_type_draw = 1 To 6
If tilenow(tile_type_draw) < 1000 Then GoTo no_draw
page = Int(tilenow(tile_type_draw) / 1000)
tile = ((tilenow(tile_type_draw) Mod 1000) - 1)
If page = 22 Then
temp_tile = get_ana_tile(((tilenow(tile_type_draw) Mod 1000)), ANACLOCK_SET)
page = Int(temp_tile / 1000)
tile = (temp_tile Mod 1000) - 1
End If
'masks
'i = BitBlt(mapeditor.Picture1.hdc, xnow, ynow, tile_width, tile_Height, masks(page), ((tile Mod temtiles_ud) * (tile_width + 2)) + 1, ((tile \ temtiles_rl) * (tile_Height + 2)) + 1, SRCAND)
'i = BitBlt(mapeditor.Picture1.hdc, xnow, ynow, tile_width, tile_Height, Graphics.Graphics_pages(page).hdc, ((tile Mod temtiles_ud) * (tile_width + 2)) + 1, ((tile \ temtiles_rl) * (tile_Height + 2)) + 1, SRCINVERT)
i = BitBlt(mapeditor.Picture1.hdc, xnow, ynow, tile_width, tile_Height, Graphics.Graphics_pages(page + 9).hdc, ((tile Mod temtiles_ud) * (tile_width + 2)) + 1, ((tile \ temtiles_rl) * (tile_Height + 2)) + 1, SRCAND)
i = BitBlt(mapeditor.Picture1.hdc, xnow, ynow, tile_width, tile_Height, Graphics.Graphics_pages(page).hdc, ((tile Mod temtiles_ud) * (tile_width + 2)) + 1, ((tile \ temtiles_rl) * (tile_Height + 2)) + 1, SRCINVERT)


no_draw:
'mapeditor.Picture1.Refresh
Next tile_type_draw

tiley = tiley + 1
Next 'ynow
Next 'xnow
If mapeditor.Gridonoff.Value = 1 Then draw_grid
'mapeditor.Picture1.Refresh
Exit Sub
End Sub


Some intresting notes:
The reason there are four BitBlts is because the first two will draw the tile using a mask in a DC (Masks(page)), The second two will draw the tile using a mask that is in a picture box. I would like to swich over to all DCs and no Picture boxes. I have decided to tackle the masks first. Now the intresting part is that when I use both picture boxes and no DCs it is faster! I don't understand why... I thought that DCs were better! Good luck to any one who thinks they can handle this problem :) -way out of my leage...
0
Comment
Question by:rpg_rpg
6 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
Comment Utility
Pictures are optimized to display stuff so unless you are able to some more optimizations that what Pictures already give you, you won't be able to beat it.

Here's how you can though. First, think of the screen as a grid of pixels. The first step to optimizing the drawing of the pixels is to make sure you don't ever draw any pixel twice. Drawing on the screen takes a long time and if you can stop drawing any pixel twice, you would have saved a lot of screen refresh.

But how do you do this, you ask? Go through all of your layers and find out which areas overlap. For those that overlap, only draw the topmost layers. For the other layers that overlap, these will be hidden and can be removed from the updating of that layer. So for example, if you have a bottom layer from (0, 0) - (40, 40) and another top layer from (20, 20) - (60, 60). This takes three updates: (0, 0) - (40, 20); (0, 20) - (20, 40); (20, 20) - (60, 60). Even though it takes a bit to calculate this, the speed is more than made up for in the screen update. The speed up is more dramatic with more layers.
0
 

Author Comment

by:rpg_rpg
Comment Utility
I may not be understanding you corectly but how I see it I can't do what you say because the reason I use layers is to give the effect of backround. I can put a tree on grass with out drawing a tile with grass and a tree. But anyway I don't understand what I can do if I need the tiles below... Thanks for the help.
0
 
LVL 2

Expert Comment

by:VBDesigns
Comment Utility
If you can, locate my book "Visual Basic 4 Network Gaming Adventure Set" (published by Coriolis).  While it's old, one of the games in the book (DirectConflict!) shows how to do multi-object bltting very quickly to a native VB hDC.

Good luck!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Expert Comment

by:LadyVyxen
Comment Utility
Recoding isn't the way for you to go, I believe. But there are ways and means. Have you thought of using DirectX?
Blitting using DirectDraw is 12 times faster, on average, than BitBlt, and the function is called more or less the same way.
To test this, try out PolyMedia's DirectX ActiveX control, which is available from: http://www.download.com/PC/Result/TitleDetail/0,4,0-62067,1000.html

Huggably yours,
Vyxen.

0
 
LVL 1

Accepted Solution

by:
Vingamel earned 130 total points
Comment Utility
Try using Imageboxes instead of Pictureboxes.  I also have a similar-sounding game as yours, and found that using imageboxes was faster, plus able to see tiles below when tiles above had transparent pixels.  Also, if you aren't already, convert any BMP images to Gif or JPG.  My game is more of a strategy game (in which high-speed graphics are negligible), however, and what I may find a satisfactory speed may be quite unsatisfactory for you.  
0
 
LVL 1

Expert Comment

by:Vingamel
Comment Utility
Thanks and holler if you need any more ideas or help, seeing that we are working on similar types of games.  --Vingamel
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now