Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Problem with canvas when using createNativeThread.

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
DaSpamer
Grandmaster Cheater Supreme
Reputation: 52

Joined: 13 Sep 2011
Posts: 1578

PostPosted: Thu Mar 13, 2014 12:06 pm    Post subject: Problem with canvas when using createNativeThread. Reply with quote

Hey,
When I use createNativeThread to render pixels it does not operate well, it skips 'pixels' only if I slow it down it starting to work bit better.

If code of what I'm doing is needed message me (650 kb file... with 2 images parsed as tables).

_________________
HEY Hitler
Do you get lazy when making trainers?
Well no more!
My CETrainer will generate it for you in seconds, so you won't get lazy! Very Happy

http://forum.cheatengine.org/viewtopic.php?t=564919
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25295
Location: The netherlands

PostPosted: Thu Mar 13, 2014 12:19 pm    Post subject: Reply with quote

Do not access the gui from other threads. Use threadobject.synchronize(function) to call the gui code in the main thread

You are free to render to your own bitmap object using threads and then display that on the canvas using the main thread

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
DaSpamer
Grandmaster Cheater Supreme
Reputation: 52

Joined: 13 Sep 2011
Posts: 1578

PostPosted: Thu Mar 13, 2014 12:28 pm    Post subject: Reply with quote

I was updating the pixels..
And when updating it kinda slows down the program a lot.
Code:
createNativeThread(   function (selfThread)
                        while 1==1 do
                           if not revert then
                              if (math.random()>=0.80) then
                                 mix2 = not mix2;
                              end
                              counter = 0;
                              for i,table in pairs(Flashacking_IMG2) do
                                 local Table =Flashacking_IMG2[i]
                                 for _,color in pairs(Table) do
                                    for x=_,_+ 8 do
                                       if x<=#Table then
                                          local color
                                          if mix2 then
                                             color = ColorTable_1[Table[x]]
                                          else
                                             color = ColorTable_2[Table[x]]
                                          end
                                          if color~=16777215 then
                                             canvas_setPixel(canvas, i, x, color)
                                          end
                                       end
                                    end
                                    if (counter == 5) then
                                       sleep(1); -- to slow down a bit, and avoiding skips
                                       counter = 0;
                                    end
                                    counter = counter + 1;
                                 end
                              end
                              revert = not revert;
                           end
                        end
                     end);

_________________
HEY Hitler
Do you get lazy when making trainers?
Well no more!
My CETrainer will generate it for you in seconds, so you won't get lazy! Very Happy

http://forum.cheatengine.org/viewtopic.php?t=564919
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25295
Location: The netherlands

PostPosted: Thu Mar 13, 2014 1:58 pm    Post subject: Reply with quote

yeah, don't call any gui related functions inside a thread

Flashacking_IMG2 is a gui object, and the canvas is belong to that image, so it turns into a gui call

what you could try instead is create a bitmap of the same size as as the TImage object (bmp=createBitmap(Flashacking_IMG2.Width, Flashacking_IMG2.Height)

then render to that bmp's canvas which is off-screen, so no gui, and when done render the image to the gui using the gui thread

example:
Code:

--create an offscreen bitmap
if bmp==nil then
  bmp=createBitmap(Flashacking.IMG2.Width,Flashacking.IMG2.Height)
end

bmp.Canvas.Brush.Color=0x000000
bmp.Canvas.clear()

function present()
  --called from the main thread
  Flashacking.IMG2.Canvas.draw(0,0,bmp)
end

function drawthread(self)
  local i
  math.randomseed( os.time() )

  while 1==1 do

    for i=1, 10 do --you can make this take as long as you wish, it won't block the gui (just lowers the framerate)
      bmp.Canvas.setPixel(math.random()*Flashacking.IMG2.Width, math.random()*Flashacking.IMG2.Height, math.random(0xffffff))
    end

  --present the image

    thread_synchronize(self, present)  --self.synchronize doesn't work in 6.3, but will in 6.3+

    --optionally
    --bmp.Canvas.clear()
  end
end

createNativeThread(drawthread)

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
DaSpamer
Grandmaster Cheater Supreme
Reputation: 52

Joined: 13 Sep 2011
Posts: 1578

PostPosted: Thu Mar 13, 2014 2:47 pm    Post subject: Reply with quote

Thanks for the example.
Wasn't sure exactly about the bitmap, but now that I am, It'll help me a lot!!.

_________________
HEY Hitler
Do you get lazy when making trainers?
Well no more!
My CETrainer will generate it for you in seconds, so you won't get lazy! Very Happy

http://forum.cheatengine.org/viewtopic.php?t=564919
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites