Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB too slow or I'm a bad programmer!

Posted on 1999-01-26
6
Medium Priority
?
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1470795
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
ID: 1470796
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
ID: 1470797
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Expert Comment

by:LadyVyxen
ID: 1470798
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 520 total points
ID: 1470799
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
ID: 1470800
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

618 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