how to use draw_getpixel_ext?

Hello,

I'm making a quizz game and I made a slot machine to pick a color which is on the middle of the "machine" and I want to get that color's RGB's and write a code like, if RGB's = stateA { QuestionType = 1; }
 

Attachments

hippyman

Member
That is extremely slow. Don't use draw_getpixel for actual game stuff.

If you want to get individual pixel color data you can draw the sprite to a surface or just use the application_surface and then get the buffer of the surface.

Here's a quick example using the entire application_surface

Code:
var bytes = surface_get_width(application_surface) * surface_get_height(application_surface) * 4;//w*h == total pixels and each pixel has 4 bytes
var buffer = buffer_create(bytes,buffer_fast,1);
buffer_get_surface(buffer,application_surface,0,0,0);

for (var i = 0; i < bytes; i+=4) {//buffer_get_surface gets pixel data in BGRA format
    var blue = buffer_peek(buffer,i,buffer_u8);
    var green = buffer_peek(buffer,i+1,buffer_u8);
    var red = buffer_peek(buffer,i+2,buffer_u8);
    var alpha = buffer_peek(buffer,i+3,buffer_u8);
}

buffer_delete(buffer);

If you notice that this is still running slow then try creating your own surface and only draw the part of the application_surface that you need to check and plug in that new surface in place of the application_surface. Just don't forget to free the surface after you delete the buffer so you don't create a memory leak.

This process is pretty fast though. I can autocrop a 4k sprite in 3 seconds.
 
I currently don't know application surface stuff and buffers but I'll just do this; "I have a slot machine that has colors in it. You press the button and colors spin then it stops and I get the color in the middle"
 
F

Fishman1175

Guest
https://docs.yoyogames.com/source/dadiospice/002_reference/drawing/colour and blending/draw_getpixel.html

Regular draw_getpixel() is probably what you want. (No _ext)
The docs have a pretty good example. You pick an x and a y on screen that you want the pixel data. The function returns a number, and this number is actually an encoded colour.

Code:
var col=draw_getpixel(room_width/2,room_height/2);
if (col==make_colour_rgb(255,0,0) //red
{

}
And yeah, there are loads of better ways to do this not using draw_getpixel or surfaces or buffers. But I think it's good to help people learn things their way when they start.
 

hippyman

Member
Well you should definitely read into both of them as they can be extremely useful. But I also just realized I over-complicated things.
You can just use surface_getpixel which is surprisingly fast

Code:
if (surface_exists(application_surface)) {
    color = surface_getpixel(application_surface,mouse_x,mouse_y);
}
The code snippet above will return the color that the mouse is on. I did this on a 1280x720 room and was getting 1000+ fps
 
Well you should definitely read into both of them as they can be extremely useful. But I also just realized I over-complicated things.
You can just use surface_getpixel which is surprisingly fast

Code:
if (surface_exists(application_surface)) {
    color = surface_getpixel(application_surface,mouse_x,mouse_y);
}
The code snippet above will return the color that the mouse is on. I did this on a 1280x720 room and was getting 1000+ fps
I didn't set the application surface to anything, would that be a problem? or what changes between view in room and application surface? Is apllication surface port on screen?
 
I had figured it out like this
Code:
var c = draw_getpixel(x,y);
var r = colour_get_red(c);
var g = colour_get_green(c);
var b = colour_get_blue(c);

if ( (r == 255) && (g == 255) && (b == 255) )
 {
  QuestionType = WennWeil;
 }

if ( (r == 100) && (g == 100) && (b == 100) )
 {
  QuestionType = SomethingElse;
 }
 

hippyman

Member
I didn't set the application surface to anything, would that be a problem? or what changes between view in room and application surface? Is apllication surface port on screen?
The application_surface is what you're seeing. It's automatically created and is the default surface being drawn too. You can use it with the surface functions the same as you would with surfaces that you created.
 
Top